本文介绍了例外的httpclient.execute到web服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试发送Web服务器上的JSON数据并获得通过这种方法应对。

I try to send JSON data on web server and get response by this method.

public JSONObject postOnServer(JSONObject json) {

    HttpResponse response;
    JSONObject res = null;

    HttpClient httpclient = new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 10000);
    HttpPost httpPost = new HttpPost(PATH);

    try {

        //StringEntity stringEntity = new StringEntity("json", String.valueOf(json));
        ByteArrayEntity postMessage = new ByteArrayEntity(json.toString().getBytes("UTF8"));
        httpPost.setEntity(postMessage);
        response = httpclient.execute(httpPost);

        if (response.getStatusLine().getStatusCode() == 200) {

            InputStream instream = response.getEntity().getContent();
            try {
                res = new JSONObject(convertStreamToString(instream));
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                Log.i("Response JSON", e.toString());
            }

        } else {

            res = new JSONObject();
            try {
                res.put("error", "Unable to load page.");
            } catch (JSONException e) {
                // TODO Auto-generated catch block
            }

        }
    } catch (IOException ex) {

        Log.i("Connection.postOnServer", "1.try exception: "+ex);
        res = new JSONObject();
        try {
            res.put("error", "Synchronization is not enable: Connection has aborted.");
        } catch (JSONException e) {
            Log.i("Connection.postOnServer", "put exception: "+e);
        }

    }

    return res;
}

但它总是在行结束 =响应httpclient.execute(httpPost); 与此异常

05-24 10:58:51.635: E/AndroidRuntime(4709): FATAL EXCEPTION: main
05-24 10:58:51.635: E/AndroidRuntime(4709): android.os.NetworkOnMainThreadException
05-24 10:58:51.635: E/AndroidRuntime(4709):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
05-24 10:58:51.635: E/AndroidRuntime(4709):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
05-24 10:58:51.635: E/AndroidRuntime(4709):     at libcore.io.IoBridge.connectErrno(IoBridge.java:138)
05-24 10:58:51.635: E/AndroidRuntime(4709):     at libcore.io.IoBridge.connect(IoBridge.java:112)
05-24 10:58:51.635: E/AndroidRuntime(4709):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
05-24 10:58:51.635: E/AndroidRuntime(4709):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
05-24 10:58:51.635: E/AndroidRuntime(4709):     at java.net.Socket.connect(Socket.java:842)
05-24 10:58:51.635: E/AndroidRuntime(4709):     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
05-24 10:58:51.635: E/AndroidRuntime(4709):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
05-24 10:58:51.635: E/AndroidRuntime(4709):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-24 10:58:51.635: E/AndroidRuntime(4709):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-24 10:58:51.635: E/AndroidRuntime(4709):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
05-24 10:58:51.635: E/AndroidRuntime(4709):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-24 10:58:51.635: E/AndroidRuntime(4709):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-24 10:58:51.635: E/AndroidRuntime(4709):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)

有一个人的想法,如何解决它?口渴尝试,如果我使用捕捉(IOException异常),aplication是destroyd。如果我使用赶上(例外)aplication去与任何操作。我应该怎么办发送数据?

Have someone idea, how to fix it? On thirst try if i use catch(IOException), aplication is destroyd. If I use catch(Exception) aplication go on with no action. What should I do to send data?

推荐答案

Android不让你运行在用户界面线程网络请求。这是为了阻止干扰了接口,而它们的执行请求。您需要执行一个单独的线程的请求。这里有一个例子:

Android doesn't let you run network requests on the user interface thread. This is to stop the requests from jamming up the interface while they execute. You need to execute the request in a separate thread. Here's an example:

new Thread(new Runnable() {
    //Thread to stop network calls on the UI thread
    public void run() {
        //Request the HTML
        try {
            HttpParams httpParameters = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParameters, TIMEOUT_CONNECTION);
            HttpConnectionParams.setSoTimeout(httpParameters, TIMEOUT_SOCKET);

            HttpClient client = new DefaultHttpClient(httpParameters);
            HttpGet request = new HttpGet(getString(R.string.url));
            HttpResponse response = client.execute(request);

            //Do something with the response
        }
        catch (IOException e) {
            Log.e("Tag", "Could not get HTML: " + e.getMessage());
        }
    }
}).start();

这篇关于例外的httpclient.execute到web服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 08:34