android - HTTP Post 请求在真实设备上工作而不在 Android 模拟器上工作

标签 android http-post

我正在向具有一些 php 脚本的 Web 服务器发出一个 post 请求以返回一些数据。

当我在真实手机中运行代码时,代码运行良好。在模拟器上测试时,它不起作用。它抛出“NULL POINTER EXCEPTION”。 exception.getMessage() 告诉 NULL

我还尝试通过直接在模拟器中从浏览器应用程序请求 url 来访问此服务器,并且成功了。当然我得到了没有发送任何发布数据的错误,因为我没有发送任何发布参数但它是可以访问的。我一开始遇到证书错误,但在我单击“确定”后它起作用了。

尝试连接到我的开发机器上的本地服务器 (10.0.2.2) 时,我遇到了同样的问题。我以为问题出在配置上,所以我将它上传到网络服务器,但仍然遇到同样的问题:

public String postData(ArrayList<String> params) {

    String responseText=null;

    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(SERVERURL);
    Log.i(TAG, "Posting HttpPost .. . ");
    //goes well till this line

    try {
    // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);

        nameValuePairs.add(new BasicNameValuePair("long", params.get(0)));
        nameValuePairs.add(new BasicNameValuePair("lat", params.get(1)));


        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

        Log.i(TAG, "Posting  httpclient.execute .. . ");

        responseText = convertStreamToString(response.getEntity().getContent());
        //responseText = EntityUtils.toString(response.getEntity());
        if (responseText !=null) {

            Log.i(TAG,"PRESPONSE TEXT:"+ responseText);
        }                       
        else
            Log.i(TAG," Entity RESPONSE is NULL");

    } catch (ClientProtocolException e) {

        Log.i(TAG, " POST ClientProtocolException " +e.getMessage());
    } catch (IOException e) {
        Log.i(TAG, " POST IOException " +e.getMessage());
    } catch (Exception el) {
        Log.i(TAG, " POST Exception " +el.getMessage());
    }

    return responseText;
} 

最佳答案

通过制作另一个针对另一个平台版本的模拟器解决了这个问题。我正在使用 Google API 开发 Android 4。我用 Google API 制作了另一个模拟器 2.3 API 10。

感谢人们在这里尝试其他模拟器或重新启动它的建议。

关于android - HTTP Post 请求在真实设备上工作而不在 Android 模拟器上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12568261/

相关文章:

android - SmartTabLayout 厚度

java - 如何实现 hashCode 和 equals 方法从 ArrayList 中删除重复项

c++ - 如何在不使用套接字的情况下在 C++ 中发送 POST 请求

android - 使用okhttp上传图片到服务器

java - Google NFC API 和 Open NFC API 之间的区别

java - ByteArray 到intentService 中的byte[]?

android - 无法解析符号 'MyApi'

android - HttpPost 不工作!

java - 从Android到java服务器(jetty)的httpPost请求: parameters get lost?

ruby - Arduino POST ERROR 错误请求行 `'