当我使用http服务时,为什么不能得到响应

时间:2018-04-12 11:39:13

标签: android http service

在我的应用程序中,我正在使用Http服务,如下所示:

 private void sendContacts() {
        try {
            contwithseparator = URLEncoder.encode(contwithseparator, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        try {    
            HttpClient httpClient = new DefaultHttpClient();
            HttpGet httpPost = new HttpGet("http://v.filmy.in/events/register/contacts.php?email=vinu@gmail.in&contacts="+contwithseparator);
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            String responseString = EntityUtils.toString(entity);
            JSONArray jsonarray = new JSONArray(responseString);
            for (int i = 0; i < jsonarray.length(); i++) {
                JSONObject jsonobject = jsonarray.getJSONObject(i);
                String name = jsonobject.getString("name");
                String phn = jsonobject.getString("mobile");                        
            }  
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

但是,当我尝试点击URL时,它正在点击不会给出任何回复 这是我的日志:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.system.costfinder, PID: 22355
                  android.os.NetworkOnMainThreadException
                      at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1448)
                      at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:102)
                      at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:90)
                      at java.net.InetAddress.getAllByName(InetAddress.java:787)
                      at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:142)
                      at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:169)
                      at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124)
                      at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:366)
                      at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560)
                      at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492)
                      at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470)
                      at com.example.system.costfinder.MainActivity.sendContacts(MainActivity.java:165)
                      at com.example.system.costfinder.MainActivity.access$000(MainActivity.java:49)
                      at com.example.system.costfinder.MainActivity$1.onClick(MainActivity.java:80)
                      at android.view.View.performClick(View.java:6256)
                      at android.view.View$PerformClick.run(View.java:24701)
                      at android.os.Handler.handleCallback(Handler.java:789)
                      at android.os.Handler.dispatchMessage(Handler.java:98)
                      at android.os.Looper.loop(Looper.java:164)
                      at android.app.ActivityThread.main(ActivityThread.java:6541)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Disconnected from the target VM, address: 'localhost:8681', transport: 'socket'

我无法理解发生了什么,即使我无法理解日志也请帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

当应用程序尝试在其主线程上执行网络操作时,抛出此异常。为避免这种情况,我们可以使用线程,执行器或AsyncTask

来处理它
Executors.newSingleThreadExecutor().submit(new Runnable() {
    @Override
    public void run() {
         HttpClient httpClient = new DefaultHttpClient();
            HttpGet httpPost = new HttpGet("http://v.filmy.in/events/register/contacts.php?email=vinu@gmail.in&contacts="+contwithseparator);
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            String responseString = EntityUtils.toString(entity);
            JSONArray jsonarray = new JSONArray(responseString);
            for (int i = 0; i < jsonarray.length(); i++) {
                JSONObject jsonobject = jsonarray.getJSONObject(i);
                String name = jsonobject.getString("name");
                String phn = jsonobject.getString("mobile");                        
            }  
    }
});

答案 1 :(得分:0)

这不是答案,而是一个建议。查看http://square.github.io/retrofit/

它可以防止您拥有的代码混乱,易于使用。可测试的。