我是Android的新手,我尝试了一个简单的HTTP Post,我有一个工作,但成为了你不能在主类中发布的错误信息。经过一些谷歌研究,我找到了另一种选择 - 异步任务 - 但我不能完全理解为什么这个简单的样本不起作用。
任何人都可以帮助我吗?
提前谢谢。
new MyAsyncTask().execute().toString();
}
class MyAsyncTask extends AsyncTask<String, Integer, Double> {
// private ProgressBar pb;
//pb=(ProgressBar);
//pb.setVisibility(View.GONE);
@Override
protected Double doInBackground(String... params) {
postData(params[0]);
return null;
}
protected void onPostExecute(Double result){
// pb.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "command sent",
Toast.LENGTH_LONG).show();
}
// protected void onProgressUpdate(Integer... progress){
// pb.setProgress(progress[0]);
//}
public void postData(String valueIWantToSend) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("url(got a real one here)");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("myHttpData", valueIWantToSend));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// Log.i("CATCH Read from server", "error");
} catch (IOException e) {
// Log.i("CATCH Read from server", "error");
}
}
}
11-11 15:08:32.768 357-394 /? E / lights:write_int无法打开/ sys / class&gt; / sec / sec_touchkey / brightness
11-11 15:10:45.503 110-259 /? E / SurfaceFlinger:SurfaceFlinger translucent = 1&gt; isOpaque = 0 isExternalDisplayLayer = 0 isExternalBlockLayer0
11-11 15:04:57.643 28774-28790 /? E / AndroidRuntime:FATAL EXCEPTION:AsyncTask#1 java.lang.RuntimeException:执行doInBackground()时发生错误 在android.os.AsyncTask $ 3.done(AsyncTask.java:299) at java.util.concurrent.FutureTask $ Sync.innerSetException(FutureTask.java:273) at java.util.concurrent.FutureTask.setException(FutureTask.java:124) at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:307) 在java.util.concurrent.FutureTask.run(FutureTask.java:137) 在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:230) at&gt; java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) at&gt; java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:569) 在java.lang.Thread.run(Thread.java:856) 引起:java.lang.IllegalStateException:目标主机不能为null,或者在&gt;参数中设置。 scheme = null,host = null,path = vlothopc.dyndns-home.com / mobile.asp at&gt; org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.j&gt; ava:591) at&gt; org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293>) at&gt; org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) at&gt; org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) at&gt; org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) at&gt; com.example.miniapp2.MainActivity $ 1 $ MyAsyncTask.postData(MainActivity.java:310) at&gt; com.example.miniapp2.MainActivity $ 1 $ MyAsyncTask.doInBackground(MainActivity.java:284) at&gt; com.example.miniapp2.MainActivity $ 1 $ MyAsyncTask.doInBackground(MainActivity.java:276) 在android.os.AsyncTask $ 2.call(AsyncTask.java:287) at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:305) 在java.util.concurrent.FutureTask.run(FutureTask.java:137) 在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:230) at&gt; java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) at&gt; java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:569) 在java.lang.Thread.run(Thread.java:856)
答案 0 :(得分:0)
您的日志中有以下异常消息:
Target host must not be null, or set in >parameters. scheme=null, host=null
这可能来自初始化HttpPost对象时放置的主机值。你忘了指定我认为的协议。
您网址的确切价值是多少?如果尚未设置,请尝试使用'http://'。
您还应该指定内容类型,长度等,但不确定这是否需要作为协议。
答案 1 :(得分:0)
请参阅http://developer.android.com/reference/android/os/AsyncTask.html
new MyAsyncTask().execute().toString();
//changed to
new MyAsyncTask().execute("value 1");
请始终查看varargs String... params
的长度。