Android HTTP POST无法识别正确的网址

时间:2014-04-17 00:24:56

标签: java android http post

很抱歉发布一个很可能已经得到解答的问题,但我一直在撕毁其他帖子和教程,但仍无法解决这个问题。

我已经编写了一个虚拟应用来练习从android发送帖子请求,但我似乎无法让它发挥作用。所有它应该做的是,在按钮单击时,发送一个帖子到httpbin.org/post,然后显示TextView中返回的内容。

以下是MainActivity的代码:

package com.getthisshit.app;



public class MainActivity extends ActionBarActivity {

TextView text;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    text = (TextView) findViewById(R.id.text);

}

public void Go(View view){
    connect doit = new connect(this);
    doit.execute();
}

private class connect extends AsyncTask<Void, Void, String>{

    private Context context;

    public connect(Context context){
        this.context = context;
    }

    @Override
    protected String doInBackground(Void... arg0){


        List<NameValuePair> userInfo = new ArrayList<NameValuePair>(2);
        String URL = "http://httpbin.org/post";
        String response = "no response";

        HttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;
        HttpPost httpPost = new HttpPost(URL);

        userInfo.add(new BasicNameValuePair("email", "bad@man.com"));
        userInfo.add(new BasicNameValuePair("password", "SayWhatAgain"));

        try{
            httpPost.setEntity(new UrlEncodedFormEntity(userInfo));
            httpResponse = httpClient.execute(httpPost);
            httpEntity = httpResponse.getEntity();
            response = EntityUtils.toString(httpEntity);
        } catch (UnsupportedEncodingException e){
            e.printStackTrace();
        } catch (ClientProtocolException e){
            e.printStackTrace();
        } catch (IOException e) {
             e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }

        return response;

    }

    @Override
    protected void onPostExecute(String response){
        //Toast.makeText(context , response, Toast.LENGTH_SHORT).show();
        text.setText(response);
    }
}

}

按下按钮时,这是logcat:

04-16 20:01:55.710  16400-16400/com.getthisshit.app I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:316>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_2.6.04.03.00.109.001_msm8960_JB_2.6_CL3908834_release_AU (CL3908834)
    OpenGL ES Shader Compiler Version: 17.01.09
    Build Date: 07/24/13 Wed
    Local Branch: mybranch1299835
    Remote Branch: quic/jb_3.1
    Local Patches: NONE
    Reconstruct Branch: AU_LINUX_ANDROID_JB_2.6.04.03.00.109.001 +  NOTHING
04-16 20:01:55.720  16400-16400/com.getthisshit.app W/Adreno-GSL﹕ <gsl_ldd_control:388>: ioctl fd 48 code 0xc01c0934 (IOCTL_KGSL_GPUMEM_ALLOC_ID) failed: errno 25 Unknown ioctl code
04-16 20:01:55.880  16400-16400/com.getthisshit.app D/qdmemalloc﹕ ion: Mapped buf base:0x6c72b000 size:8355840 offset:0 fd:54
04-16 20:01:55.880  16400-16400/com.getthisshit.app D/qdmemalloc﹕ ion: Mapped buf base:0x66fde000 size:4096 offset:0 fd:55
04-16 20:02:03.969  16400-16400/com.getthisshit.app D/qdmemalloc﹕ ion: Mapped buf base:0x6d023000 size:8355840 offset:0 fd:57
04-16 20:02:03.969  16400-16400/com.getthisshit.app D/qdmemalloc﹕ ion: Mapped buf base:0x66fe5000 size:4096 offset:0 fd:58
04-16 20:02:04.019  16400-16400/com.getthisshit.app D/qdmemalloc﹕ ion: Mapped buf base:0x6d93b000 size:8355840 offset:0 fd:59
04-16 20:02:04.019  16400-16400/com.getthisshit.app D/qdmemalloc﹕ ion: Mapped buf base:0x687b8000 size:4096 offset:0 fd:60
04-16 20:02:04.109  16400-16539/com.getthisshit.app D/libc﹕ [NET] getaddrinfo+,hn 11(0x6874747062696e),sn(),family 0,flags 4
04-16 20:02:04.109  16400-16539/com.getthisshit.app D/libc﹕ [NET] getaddrinfo-,err=8
04-16 20:02:04.109  16400-16539/com.getthisshit.app D/libc﹕ [NET] getaddrinfo+,hn 11(0x6874747062696e),sn(),family 0,flags 1024
04-16 20:02:04.109  16400-16539/com.getthisshit.app D/libc﹕ [NET] getaddrinfo-, 1
04-16 20:02:04.109  16400-16539/com.getthisshit.app D/libc﹕ [NET] getaddrinfo_proxy+
04-16 20:02:30.137  16400-16539/com.getthisshit.app D/libc﹕ [NET] getaddrinfo_proxy-
04-16 20:02:30.147  16400-16539/com.getthisshit.app W/System.err﹕ java.net.UnknownHostException: Unable to resolve host "httpbin.org": No address associated with hostname
04-16 20:02:30.157  16400-16539/com.getthisshit.app W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:434)
04-16 20:02:30.157  16400-16539/com.getthisshit.app W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:239)
04-16 20:02:30.157  16400-16539/com.getthisshit.app W/System.err﹕ at java.net.InetAddress.getAllByName(InetAddress.java:214)
04-16 20:02:30.157  16400-16539/com.getthisshit.app W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
04-16 20:02:30.157  16400-16539/com.getthisshit.app W/System.err﹕ at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-16 20:02:30.167  16400-16539/com.getthisshit.app W/System.err﹕ at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-16 20:02:30.167  16400-16539/com.getthisshit.app W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:367)
04-16 20:02:30.167  16400-16539/com.getthisshit.app W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
04-16 20:02:30.167  16400-16539/com.getthisshit.app W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:519)
04-16 20:02:30.177  16400-16539/com.getthisshit.app W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:497)
04-16 20:02:30.177  16400-16539/com.getthisshit.app W/System.err﹕ at com.getthisshit.app.MainActivity$connect.doInBackground(MainActivity.java:77)
04-16 20:02:30.177  16400-16539/com.getthisshit.app W/System.err﹕ at com.getthisshit.app.MainActivity$connect.doInBackground(MainActivity.java:51)
04-16 20:02:30.177  16400-16539/com.getthisshit.app W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-16 20:02:30.177  16400-16539/com.getthisshit.app W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-16 20:02:30.187  16400-16539/com.getthisshit.app W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-16 20:02:30.187  16400-16539/com.getthisshit.app W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-16 20:02:30.187  16400-16539/com.getthisshit.app W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-16 20:02:30.197  16400-16539/com.getthisshit.app W/System.err﹕ at java.lang.Thread.run(Thread.java:849)
04-16 20:02:30.197  16400-16539/com.getthisshit.app W/System.err﹕ Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
04-16 20:02:30.197  16400-16539/com.getthisshit.app W/System.err﹕ at libcore.io.Posix.getaddrinfo(Native Method)
04-16 20:02:30.197  16400-16539/com.getthisshit.app W/System.err﹕ at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
04-16 20:02:30.197  16400-16539/com.getthisshit.app W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:415)
04-16 20:02:30.197  16400-16539/com.getthisshit.app W/System.err﹕ ... 17 more
04-16 20:02:30.277  16400-16400/com.getthisshit.app D/qdmemalloc﹕ ion: Mapped buf base:0x6e133000 size:184320 offset:0 fd:63
04-16 20:02:30.277  16400-16400/com.getthisshit.app D/qdmemalloc﹕ ion: Mapped buf base:0x688cd000 size:4096 offset:0 fd:64
04-16 20:02:32.229  16400-16400/com.getthisshit.app D/qdmemalloc﹕ ion: Unmap buf  base:0x6e133000 size:184320
04-16 20:02:32.229  16400-16400/com.getthisshit.app D/qdmemalloc﹕ ion: Unmap buf  base:0x688cd000 size:4096
04-16 20:13:39.841  16400-16400/com.getthisshit.app D/qdmemalloc﹕ ion: Unmap buf  base:0x6c72b000 size:8355840
04-16 20:13:39.841  16400-16400/com.getthisshit.app D/qdmemalloc﹕ ion: Unmap buf  base:0x66fde000 size:4096
04-16 20:13:39.841  16400-16400/com.getthisshit.app D/qdmemalloc﹕ ion: Unmap buf  base:0x6d023000 size:8355840
04-16 20:13:39.841  16400-16400/com.getthisshit.app D/qdmemalloc﹕ ion: Unmap buf  base:0x66fe5000 size:4096
04-16 20:13:39.841  16400-16400/com.getthisshit.app D/qdmemalloc﹕ ion: Unmap buf  base:0x6d93b000 size:8355840
04-16 20:13:39.851  16400-16400/com.getthisshit.app D/qdmemalloc﹕ ion: Unmap buf  base:0x687b8000 size:4096

我认为这一行特别重要:

java.net.UnknownHostException:无法解析主机&#34; httpbin.org&#34;:没有与主机名关联的地址

但我的google-fu让我失望

当我将相同的帖子放入hurl.it时,我得到了正确的回复。 我在清单文件中有互联网权限 我正在使用Android 4.3测试我的HTC1上的应用程序 我在wifi上有互联网连接

提前致谢。如果我不明白这一点,我意识到这不是世界末日,但这会让我感到非常难过。

1 个答案:

答案 0 :(得分:0)

我尝试在本地托管的Web服务上执行HTTP POST时,昨天在Nexus 7上遇到了同样的错误。对我来说,当我重启设备时,错误就消失了。有点远,但它对我有用!