无法在android中调用REST API

时间:2013-12-17 08:25:56

标签: android web-services rest

我正试图通过手机拨打REST网络服务。我正在使用以下代码来完成此操作。我只有一个活动,它有一个按钮和textview。每当我点击按钮时,它会在logcat

中出现以下错误

AndroidRuntime :: at android.os.Handler.dispatchMessage(Handler.java:92)

我做错了什么?我该怎么解决这个??? 以下是我的课程。

rest.java

public class Rest extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.start_rest);

    Button button = (Button) findViewById(R.id.btn);


    button.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String url = "http://192.168.1.145/tst.php";

            RestService re = new RestService();             
            JSONObject jb =  RestService.doGet(url);

            TextView tv = (TextView) findViewById(R.id.txtView);                
            tv.setText(jb.toString());
        }
    });

  }
}

RestService.java

public class RestService {

public static JSONObject doGet(String url) {
    JSONObject json = null;

    HttpClient httpclient = new DefaultHttpClient();
    // Prepare a request object
    HttpGet httpget = new HttpGet(url);
    // Accept JSON
    httpget.addHeader("accept", "application/json");
    // Execute the request
    HttpResponse response;

    try {
        response = httpclient.execute(httpget);
        // Get the response entity
       // Log.e("myApp", "Issue is here...!");
        HttpEntity entity = response.getEntity();
        // If response entity is not null
        if (entity != null) {
            // get entity contents and convert it to string
            InputStream instream = entity.getContent();
            String result= convertStreamToString(instream);
            // construct a JSON object with result
            json=new JSONObject(result);
            // Closing the input stream will trigger connection release
            instream.close();
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // Return the json
    return json;
}

private static String convertStreamToString(InputStream is) {

    BufferedReader br = null;
    StringBuilder sb = new StringBuilder();

    String line;
    try {

        br = new BufferedReader(new InputStreamReader(is));
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return sb.toString();
}
}

2 个答案:

答案 0 :(得分:3)

尝试过你发布的代码后,我得到以下异常(我怀疑你会在logcat中找到相同的内容,如果你看的话)

12-17 10:28:26.850    2182-2182/com.example.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
    android.os.NetworkOnMainThreadException
            at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
            at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
            at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
            at java.net.InetAddress.getAllByName(InetAddress.java:214)
            at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
            at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
            at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
            at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
            at com.example.app.RestService.doGet(RestService.java:31)
            at com.example.app.MainActivity$1.onClick(MainActivity.java:28)
            at android.view.View.performClick(View.java:4084)
            at android.view.View$PerformClick.run(View.java:16966)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

你得到这个是因为你试图在主线程上通过网络做某事,这是不允许的。 (它在Google design guidelines here中提到)

在这种情况下,最好的解决方案是将服务调用移动到AsyncTask,它在单独的线程上运行,不会阻止UI。

试试这个,我换掉了只返回日期的freely available JSON web service的网址

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.btn);


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = "http://date.jsontest.com";
                new MyAsyncTask().execute(url);
            }
        });


    }
    class MyAsyncTask extends AsyncTask<String,Void,JSONObject> {

        @Override
        protected JSONObject doInBackground(String... urls) {
            return RestService.doGet(urls[0]);
        }

        @Override
        protected void onPostExecute(JSONObject jsonObject) {
            TextView tv = (TextView) findViewById(R.id.txtView);
            tv.setText(jsonObject.toString());
        }
    }

}

答案 1 :(得分:2)

编写异步任务并在该线程中执行网络操作。

     public class myTask extends AsyncTask<String, JSONObject, Void> {

        private ProgressDialog dialog;
        private Context mContext;

        public myTask(Context mContext){
            this.mContext = mContext;

        }

        protected void onPreExecute() {
            this.dialog = new ProgressDialog(mContext);
            this.dialog.setCancelable(false);
            this.dialog.setMessage("please wait..");
            this.dialog.show();
        }

        protected JSONObject doInBackground(String... ids) {

            String url = "http://192.168.1.145/tst.php";
            RestService re = new RestService();             
            JSONObject jb =  RestService.doGet(url);

            return jb;
        }

       protected Void onPostExecute(JSONObject jb) {

            //get json data and set
        }
    }