使用HttpResponse

时间:2015-12-25 22:25:29

标签: android json web-services httpclient httpresponse

我正在尝试将JSON数据对象传递给Android应用程序中的Web服务。我有一个按钮,当我点击它时,我想将位置发送到我的网络服务。一切正常,直到最后一个命令。我从另一篇文章中得到了我的代码: How to post data to a Webservice using JSON?

它应该是有效的,所以我认为问题出在其他地方,但错误对我没有任何意义..这是我在onclick上运行的函数:

public void clickbuttonRecieve(View v) {
         new MyAsync().execute();
 }


 private class MyAsync extends AsyncTask<Void, Void, Void> {
        protected Void doInBackground(Void... tmps) {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(
                    "http://bomatec.be/webservice2.php");

            httpPost.setHeader("content-type", "application/json");
            JSONObject data = new JSONObject();
            try {
                String message;

                data.put("user", "12");
                data.put("lat", "50.8");
                data.put("lng", "4.3");


            }catch (JSONException e) {
                Log.e("MYAPP", "unexpected JSON exception", e);
            }

            try {
                StringEntity entity = new StringEntity(data.toString(), HTTP.UTF_8);
                httpPost.setEntity(entity);
            }catch(java.io.UnsupportedEncodingException e){
                Log.e("MYAPP", "UnsupportedEncodingException JSON exception", e);
            }


            try {
                HttpResponse response = httpClient.execute(httpPost);
            }catch (IOException e){
                Log.e("MYAPP", "IOException", e);
            }
            return null;
        }
    }

错误堆栈:

 java.lang.RuntimeException: An error occured while executing doInBackground()
                                                                                                    at android.os.AsyncTask$3.done(AsyncTask.java:299)
                                                                                                    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
                                                                                                    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
                                                                                                    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
                                                                                                    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
                                                                                                    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
                                                                                                    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
                                                                                                    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
                                                                                                    at java.lang.Thread.run(Thread.java:856)
                                                                                                 Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
                                                                                                    at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
                                                                                                    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.stevengerrits.bomatec_gerrits_steven.MainActivity$MyAsync.doInBackground(MainActivity.java:177)
                                                                                                    at com.example.stevengerrits.bomatec_gerrits_steven.MainActivity$MyAsync.doInBackground(MainActivity.java:148)
                                                                                                    at android.os.AsyncTask$2.call(AsyncTask.java:287)
                                                                                                    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
                                                                                                    at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
                                                                                                    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
                                                                                                    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
                                                                                                    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
                                                                                                    at java.lang.Thread.run(Thread.java:856) 
                                                                                                 Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
                                                                                                    at libcore.io.Posix.getaddrinfo(Native Method)
                                                                                                    at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:55)
                                                                                                    at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
                                                                                                    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.stevengerrits.bomatec_gerrits_steven.MainActivity$MyAsync.doInBackground(MainActivity.java:177) 
                                                                                                    at com.example.stevengerrits.bomatec_gerrits_steven.MainActivity$MyAsync.doInBackground(MainActivity.java:148) 
                                                                                                    at android.os.AsyncTask$2.call(AsyncTask.java:287) 
                                                                                                    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
                                                                                                    at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
                                                                                                    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
                                                                                                    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
                                                                                                    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
                                                                                                    at java.lang.Thread.run(Thread.java:856) 
                                                                                                 Caused by: libcore.io.ErrnoException: getaddrinfo failed: EACCES (Permission denied)
                                                                                                    at libcore.io.Posix.getaddrinfo(Native Method) 
                                                                                                    at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:55) 
                                                                                                    at java.net.InetAddress.lookupHostByName(InetAddress.java:405) 
                                                                                                    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.stevengerrits.bomatec_gerrits_steven.MainActivity$MyAsync.doInBackground(MainActivity.java:177) 
                                                                                                    at com.example.stevengerrits.bomatec_gerrits_steven.MainActivity$MyAsync.doInBackground(MainActivity.java:148) 
                                                                                                    at android.os.AsyncTask$2.call(AsyncTask.java:287) 
                                                                                                    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
                                                                                                    at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
                                                                                                    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
                                                                                                    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
                                                                                                    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
                                                                                                    at java.lang.Thread.run(Thread.java:856) 

1 个答案:

答案 0 :(得分:0)

您有NetworkOnMainThreadException异常,您必须将onClick代码放入AsyncTask。 Android禁止在UI线程上进行网络操作,这是为了防止阻塞主UI消息队列。

示例代码是:

 private class MyAsync extends AsyncTask<Void, Void, Void> {
     protected Void doInBackground(Void... tmps) {
         // do your work here!!
         return null;
     }
 }

在您的onclick中 - 执行:new MyAsync().execute()