我正在使用以下代码
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://domain.com/~path/page.php");
try
{
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("param", value1));
nameValuePairs.add(new BasicNameValuePair("param2", value2));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
}
catch (Exception e)
{
e.printStackTrace();
}
我认为问题是路径中文件夹上的“〜”。我无法弄清楚我必须使用什么编码来使URL有效。我之前遇到过“ã”的问题,我不得不使用某种编码将网址转换为“http://XN--SEULEITO-XZA.COM/”,但这种编码不适用于孤立的“〜”。这就是printstack输出的内容。
10-21 11:40:02.311 19329-19329/com.veustechnology.titansaude.development W/System.err﹕ android.os.NetworkOnMainThreadException 10-21 11:40:02.321 19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1128) 10-21 11:40:02.321 19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 10-21 11:40:02.321 19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 10-21 11:40:02.321 19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at java.net.InetAddress.getAllByName(InetAddress.java:214) 10-21 11:40:02.321 19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137) 10-21 11:40:02.321 19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 10-21 11:40:02.321 19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 10-21 11:40:02.321 19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 10-21 11:40:02.321 19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 10-21 11:40:02.321 19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 10-21 11:40:02.331 19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 10-21 11:40:02.331 19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at com.veustechnology.titansaude.LoginActivity.onClick(LoginActivity.java:194) 10-21 11:40:02.331 19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at android.view.View.performClick(View.java:4377) 10-21 11:40:02.331 19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at android.view.View$PerformClick.run(View.java:18044) 10-21 11:40:02.331 19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:725) 10-21 11:40:02.331 19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:92) 10-21 11:40:02.331 19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at android.os.Looper.loop(Looper.java:137) 10-21 11:40:02.331 19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5306) 10-21 11:40:02.331 19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method) 10-21 11:40:02.331 19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:511) 10-21 11:40:02.331 19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 10-21 11:40:02.331 19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 10-21 11:40:02.341 19329-19329/com.veustechnology.titansaude.development W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
在utf-8中编码您的URL字符串
String query = URLEncoder.encode(""http://domain.com/~path/page.php"", "utf-8");
HttpPost httppost = new HttpPost(query);
答案 1 :(得分:0)
您获得NetworkOnMainThreadException
的原因是您尝试通过执行HttpRequest
来锁定主线程。 Don't do that!
改为使用AsyncTask
。
class GetResponse extends AsyncTask<Void, Void, HttpResponse> {
private Exception exception;
protected HttpResponse doInBackground(Void... params) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://domain.com/~path/page.php");
try
{
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("param", value1));
nameValuePairs.add(new BasicNameValuePair("param2", value2));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
return response;
}
catch (Exception e)
{
e.printStackTrace();
}
}
protected void onPostExecute(HttpResponse response) {
}
}
答案 2 :(得分:0)
没关系,这个太空头在上个星期五关闭了设备的wifi,忘记了它。