English 中文(简体)
甲型六氯环己烷
原标题:android connecting to json/xml api

I m试图与一个休息站联系,以检索一份JSON文件,但当我在Eclipse emulator提出申请时,该信封一开始就关闭了(“不尽人意的检测”)。 由于向httpClient发出呼吁,似乎已经结束。 执行方法。

主要活动守则如下:

public class TestRestActivity extends Activity {
    /** Called when the activity is first created. */
    TextView info;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        info = (TextView) findViewById(R.id.debug);

        String testUri = "http://www.entireweb.com/xmlquery?pz=MYAPIKEY&ip=IP&q=pizza&n=50&format=json";
        // print the uri to the screen - (not seen if rest of code runs)
        info.setText(testUri);

        HttpClient httpclient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpGet httpget = new HttpGet(testUri);


        HttpResponse response = null;
        try {
            // TODO fails on this line
            response = httpclient.execute(httpget, localContext);

            HttpEntity entity = response.getEntity();
            if (entity != null) {
                info.setText("got it");
            }

        } catch (ClientProtocolException e) {
            // Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // Auto-generated catch block
            e.printStackTrace();
        } finally {
        }

    }
}

我也把网站许可列入我的明文档案:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.john.testRes"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-permission android:name="android.permission.INTERNET" />

不太熟悉和淡化发展,而且实际上无法理解造成这一问题的原因。 使用一米的皮.是罚款,并将预期的json文件从我的浏览器中退回。 利用AVD I m可以连接因特网的浏览器,I ve用几台不同的DVD对手。

是否有任何建议引起问题? 感谢。

Edit: added errors

11-22 18:13:14.221: D/AndroidRuntime(556): Shutting down VM
11-22 18:13:14.221: W/dalvikvm(556): threadid=1: thread exiting with uncaught exception (group=0x409951f8)
11-22 18:13:14.251: E/AndroidRuntime(556): FATAL EXCEPTION: main
11-22 18:13:14.251: E/AndroidRuntime(556): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.john.testRes/org.john.testRes.TestRestActivity}: android.os.NetworkOnMainThreadException
11-22 18:13:14.251: E/AndroidRuntime(556):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
11-22 18:13:14.251: E/AndroidRuntime(556):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
11-22 18:13:14.251: E/AndroidRuntime(556):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
11-22 18:13:14.251: E/AndroidRuntime(556):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
11-22 18:13:14.251: E/AndroidRuntime(556):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-22 18:13:14.251: E/AndroidRuntime(556):  at android.os.Looper.loop(Looper.java:137)
11-22 18:13:14.251: E/AndroidRuntime(556):  at android.app.ActivityThread.main(ActivityThread.java:4340)
11-22 18:13:14.251: E/AndroidRuntime(556):  at java.lang.reflect.Method.invokeNative(Native Method)
11-22 18:13:14.251: E/AndroidRuntime(556):  at java.lang.reflect.Method.invoke(Method.java:511)
11-22 18:13:14.251: E/AndroidRuntime(556):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-22 18:13:14.251: E/AndroidRuntime(556):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-22 18:13:14.251: E/AndroidRuntime(556):  at dalvik.system.NativeStart.main(Native Method)
11-22 18:13:14.251: E/AndroidRuntime(556): Caused by: android.os.NetworkOnMainThreadException
11-22 18:13:14.251: E/AndroidRuntime(556):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
11-22 18:13:14.251: E/AndroidRuntime(556):  at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
11-22 18:13:14.251: E/AndroidRuntime(556):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
11-22 18:13:14.251: E/AndroidRuntime(556):  at java.net.InetAddress.getAllByName(InetAddress.java:220)
11-22 18:13:14.251: E/AndroidRuntime(556):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
11-22 18:13:14.251: E/AndroidRuntime(556):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
11-22 18:13:14.251: E/AndroidRuntime(556):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
11-22 18:13:14.251: E/AndroidRuntime(556):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
11-22 18:13:14.251: E/AndroidRuntime(556):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-22 18:13:14.251: E/AndroidRuntime(556):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-22 18:13:14.251: E/AndroidRuntime(556):  at org.john.testRes.TestRestActivity.onCreate(TestRestActivity.java:41)
11-22 18:13:14.251: E/AndroidRuntime(556):  at android.app.Activity.performCreate(Activity.java:4465)
11-22 18:13:14.251: E/AndroidRuntime(556):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-22 18:13:14.251: E/AndroidRuntime(556):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
11-22 18:13:14.251: E/AndroidRuntime(556):  ... 11 more
11-22 18:13:48.321: I/Process(556): Sending signal. PID: 556 SIG: 9
11-22 18:14:01.552: D/AndroidRuntime(597): Shutting down VM
最佳回答

问题是,你正试图在主线上开展网络业务。 你们需要单独地这样做。 您可使用<条码>AsyncTask或仅使用标准<条码>。

Here is a simple example:

final Handler handler = new Handler();
new Thread(new Runnable() {

     @Override
     public void run() {
          // Do something long running
          handler.post(new Runnable() {

               @Override
               public void run() {
                    // Do something on the UI Thread
               }
          });
     }
}).start();
问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签