0

我必须使用带有来自android的参数的HTTP POST请求将一些参数从客户端(android)发布到服务器端(php)。我尝试了一个在 Intranet(192.168.12.33) 中工作的示例,这是我系统的本地 IP,但在 Internet(www.dillgates.pcriot.com) 中没有工作,这是我的网站。

这是我的代码。如果我替换“192.168.2.3”而不是“www.dillgates.pcriot.com”,这是可行的

public String httppost(String param1, String param2, String file) throws JSONException
{
    ArrayList<NameValuePair> nameValuePairs = new  ArrayList<NameValuePair>();      
    nameValuePairs.add(new BasicNameValuePair("param1", param1));
    nameValuePairs.add(new BasicNameValuePair("param2", param2));
    try
    {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://192.168.2.3/5mine/GeoTracker/"+file);//Working
        //HttpPost httppost = new HttpPost("http://www.dillgates.pcriot.com/5mine/GeoTracker/"+file);//Not working
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        the_string_response = convertResponseToString(response);
    }
    catch(Exception e)
    {
        Toast.makeText(GeoTrackerActivity.this, "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show();
        System.out.println("Error in http connection "+e.toString());
    }
}

这是响应,无论我在 Log cat 中得到什么。

Logcat 错误

4

1 回答 1

0

可能是我使用 httprequest 给你我的设置。

-> 清单设置

在 Application 标签前添加两行权限

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

-> 更新政策

ThreadPolicy mThreadPolicy = new ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(mThreadPolicy);

-> 使用 HttpURLConnection

String para = "username=123&password=321"
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Language", "en-US");
connection.setRequestProperty("Content-Length", Integer.toString(para.getBytes().length));
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);

OutputStreamWriter osw = new DataOutputStream(connection.getOutputStream());
osw.writeBytes(para);
osw.flush();

BufferedReader mBufferedReader = new BufferedReader(new InputStreamReader(this.mConnection.getInputStream()));
StringBuilder mStringBuilder = new StringBuilder();
String line = "";
while((line = mBufferedReader.readLine()) != null){
    mStringBuilder.append(line);
}
String responseString = mStringBuilder.toString();
于 2013-05-20T06:10:49.943 回答