尝试将数据发布到HTTP服务器。基本上我创建了一个输入文本字段和一个按钮,所以当我输入数据时,按钮将允许我发布它。但是,每次输入数字并按下按钮时,程序都会崩溃。以下是错误。
04-18 19:11:49.497: E/AndroidRuntime(1781): FATAL EXCEPTION: AsyncTask #2
04-18 19:11:49.497: E/AndroidRuntime(1781): Process: org.example.androidsdk.demo, PID: 1781
04-18 19:11:49.497: E/AndroidRuntime(1781): java.lang.RuntimeException: An error occured while executing doInBackground()
04-18 19:11:49.497: E/AndroidRuntime(1781): at android.os.AsyncTask$3.done(AsyncTask.java:300)
04-18 19:11:49.497: E/AndroidRuntime(1781): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
04-18 19:11:49.497: E/AndroidRuntime(1781): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
04-18 19:11:49.497: E/AndroidRuntime(1781): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
04-18 19:11:49.497: E/AndroidRuntime(1781): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-18 19:11:49.497: E/AndroidRuntime(1781): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-18 19:11:49.497: E/AndroidRuntime(1781): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-18 19:11:49.497: E/AndroidRuntime(1781): at java.lang.Thread.run(Thread.java:841)
04-18 19:11:49.497: E/AndroidRuntime(1781): Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=volta.sdsu.edu/~biocom/PHP/PHPService/index.php
04-18 19:11:49.497: E/AndroidRuntime(1781): at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:591)
04-18 19:11:49.497: E/AndroidRuntime(1781): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)
04-18 19:11:49.497: E/AndroidRuntime(1781): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-18 19:11:49.497: E/AndroidRuntime(1781): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-18 19:11:49.497: E/AndroidRuntime(1781): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-18 19:11:49.497: E/AndroidRuntime(1781): at org.example.androidsdk.demo.MainActivity$MyAsyncTask.postData(MainActivity.java:96)
04-18 19:11:49.497: E/AndroidRuntime(1781): at org.example.androidsdk.demo.MainActivity$MyAsyncTask.doInBackground(MainActivity.java:68)
04-18 19:11:49.497: E/AndroidRuntime(1781): at org.example.androidsdk.demo.MainActivity$MyAsyncTask.doInBackground(MainActivity.java:1)
04-18 19:11:49.497: E/AndroidRuntime(1781): at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-18 19:11:49.497: E/AndroidRuntime(1781): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-18 19:11:49.497: E/AndroidRuntime(1781): ... 4 more
主要活动中的代码是:
package org.example.androidsdk.demo;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private EditText value;
private Button btn;
private ProgressBar pb;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
value=(EditText)findViewById(R.id.editText1);
btn=(Button)findViewById(R.id.button1);
pb=(ProgressBar)findViewById(R.id.progressBar1);
pb.setVisibility(View.GONE);
btn.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClick(View v) {
// TODO Auto-generated method stub
if(value.getText().toString().length()<1){
// out of range
Toast.makeText(this, "please enter something", Toast.LENGTH_LONG).show();
}else{
pb.setVisibility(View.VISIBLE);
new MyAsyncTask().execute(value.getText().toString());
}
}
private class MyAsyncTask extends AsyncTask<String, Integer, Double>{
@Override
protected Double doInBackground(String... params) {
// TODO Auto-generated method stub
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) {
HttpClient httpclient = new DefaultHttpClient();
// specify the URL you want to post to
HttpPost httppost = new HttpPost("volta.sdsu.edu/~biocom/PHP/PHPService/index.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("PatientId", "1"));
// nameValuePairs.add(new BasicNameValuePair("DataType", "Pulse"));
// nameValuePairs.add(new BasicNameValuePair("DataValue", "70.99"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// process exception
} catch (IOException e) {
}
}
}
public void gotoActivity(View v) {
Intent intent = new Intent(this, DetailActivity.class);
startActivity(intent);
}
public void gotoHeart(View v){
Intent intent2 = new Intent(this, HeartRateActivity.class);
startActivity(intent2);
}
public void gotoTemperature(View v){
Intent intent3 = new Intent(this, TemperatureActivity.class);
startActivity(intent3);
}
public void gotoText(View v){
Intent intent = new Intent();
intent.setAction(intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "Emergency!");
intent.setType("text/plain");
startActivity(intent);
}
}
打开另一页的最后3个按钮可以顺利运行。只是文本字段的第一部分,并在我尝试调用HTTP的发布时提交给我带来麻烦。任何帮助表示赞赏。哦,我确实在清单中启用了android.permission.INTERNET。