线程和任务AsyncTask的使用了httppost [英] Threads and Asynctask task use for httppost
问题描述
朋友们,我需要帮助到Android httppost使用的AsyncTask或线程数据到服务器
我需要将数据发送到我的服务器当我点击后button.But当我点击它的应用程序需要到下一个页面和数据需要通过为背景送process.I'm新Android.I不知道是什么正是使用这种任务(线程或Asyanctask)的。
我想这code,但它会给我的异常错误
Friends ,i need help to android httppost data to server using Asynctask or Threads I need to send data to my server when i click post button.But when i click it app need to go to next page and data need to send through as background process.I'm new to Android.I don't know what is exactly use for this kind of task (Threads or Asyanctask). I tried this code but it will give me exception error
public void startProgress(final String name) {
// Do something long
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(500);
send(name);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
new Thread(runnable).start();
}
public void send(String name)
{
// get the message from the message text box
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2:8080/Test");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
String co2 =input_field.getText().toString();
nameValuePairs.add(new BasicNameValuePair("Name", name));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Toast toast = Toast.makeText(getApplicationContext(), "Got it ", Toast.LENGTH_SHORT);
toast.show();
httpclient.execute(httppost);
input_field.setText("");
} catch(Exception e){
Toast toast2 = Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT);
toast2.show();
}
}
但如果我使用这种方式很有效。(文本在该页面的TextView项目)
but if i use it this way it works.(text is TextView item in that page)
public void startProgress(final String name) {
// Do something long
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
text.post(new Runnable() {
@Override
public void run() {
send(name);
}
});
}
};
new Thread(runnable).start();
}
在波纹管件code的什么情况可以请你这个也解释了
What happen in bellow piece of code can you please explain about this also
text.post(new Runnable() {
@Override
public void run() {
send(name);
}
});
请帮我解决这个问题。如果有更好的方法做我需要请提起过。因为它有关于Android开发非常少的经验
please help me to solve this problem.If there is better way to do my need please mentioned it .Because it have very less experience about Android development
推荐答案
您可以用做的AsyncTask
是这样的:
public class HttpPost extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... params)
{
BufferedReader inBuffer = null;
String url = "http://10.0.2.2:8080/Test";
String result = "fail";
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost request = new HttpPost(url);
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("name", params[0]));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
postParameters);
request.setEntity(formEntity);
httpClient.execute(request);
result="got it";
} catch(Exception e) {
// Do something about exceptions
result = e.getMessage();
} finally {
if (inBuffer != null) {
try {
inBuffer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
protected void onPostExecute(String page)
{
//textView.setText(page);
Toast toast = Toast.makeText(getApplicationContext(), page, Toast.LENGTH_SHORT);
toast.show();
}
}
和你需要有这个在你的主要方法
And you need to have this in your main method
new HttpPost().execute(new String[] {name});
检查了这一点。
希望这会帮助你。
Check this out. Hope this will help you.
这篇关于线程和任务AsyncTask的使用了httppost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!