Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:在AsyncTask中执行httpclient.execute时应用程序崩溃_Android - Fatal编程技术网

Android:在AsyncTask中执行httpclient.execute时应用程序崩溃

Android:在AsyncTask中执行httpclient.execute时应用程序崩溃,android,Android,我与httpclient有问题。我调试了应用程序,当调用execute函数时它崩溃了。代码如下: public class MyAsyncTask extends AsyncTask<String, String, String> { GridView mGridView; Activity mContex; public MyAsyncTask(Activity contex) { this.mContex = contex; } @Override protected

我与httpclient有问题。我调试了应用程序,当调用execute函数时它崩溃了。代码如下:

public class MyAsyncTask extends AsyncTask<String, String, String> {
GridView mGridView;
Activity mContex;

public MyAsyncTask(Activity contex) {
    this.mContex = contex;
}

@Override
protected String doInBackground(String... params) {

    //fetch data

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("localhost:3000/api/send");

    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("email", params[1]));
        nameValuePairs.add(new BasicNameValuePair("name", params[0]));
        nameValuePairs.add(new BasicNameValuePair("gender", "male"));
        nameValuePairs.add(new BasicNameValuePair("result", "lazy bee"));
        nameValuePairs.add(new BasicNameValuePair("age", "28"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }

    return "success";
}
}
有人知道解决办法吗?感谢新的HttpPost(“localhost:3000/api/send”)每个http请求都需要完全限定。也不要使用本地主机。Android不会以这种方式识别您的端点。我总是用ngrok在网上广播。切换到

new HttpPost("http://your_domain");

同时切换到Refught2,它使用java注释来编写其请求。强烈推荐。这里有一个很好的开始链接:

请提供一些错误消息。
newhttppost(“localhost:3000/api/send”)。你忘了协议。所以它应该是
newhttppost(“http://localhost:3000/api/send");。但是,除非服务器与应用程序运行在同一设备上,否则localhost不会运行。请告诉你的设置。您要连接的内容。
调用execute时会崩溃。那么你没有发现哪一个例外?好的,非常感谢,我完全忘记了协议!使用ngrok来广播tunnelgreat你不能用ngrok出错,因为它会生成一个竞争URL来同时使用http和https。@greenapps你在说什么?像这样做ngrok http你的端口,转到localhost:4040,然后你会看到两个URL,复制http或https的完全限定路径。然后替换android studio中的一个。ngrok对于可视化路由的请求和响应非常有用。
new HttpPost("http://your_domain");