我正在尝试检查服务器是否可用,并且收到此错误:
01-17 08:02:42.140: E/AndroidRuntime(10317): FATAL EXCEPTION: AsyncTask #1
01-17 08:02:42.140: E/AndroidRuntime(10317): java.lang.RuntimeException: An error occured while executing doInBackground()
01-17 08:02:42.140: E/AndroidRuntime(10317): at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-17 08:02:42.140: E/AndroidRuntime(10317): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-17 08:02:42.140: E/AndroidRuntime(10317): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-17 08:02:42.140: E/AndroidRuntime(10317): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-17 08:02:42.140: E/AndroidRuntime(10317): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-17 08:02:42.140: E/AndroidRuntime(10317): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-17 08:02:42.140: E/AndroidRuntime(10317): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-17 08:02:42.140: E/AndroidRuntime(10317): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-17 08:02:42.140: E/AndroidRuntime(10317): at java.lang.Thread.run(Thread.java:856)
01-17 08:02:42.140: E/AndroidRuntime(10317): Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=www.novusmedia.co.za
01-17 08:02:42.140: E/AndroidRuntime(10317): at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:591)
01-17 08:02:42.140: E/AndroidRuntime(10317): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)
01-17 08:02:42.140: E/AndroidRuntime(10317): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-17 08:02:42.140: E/AndroidRuntime(10317): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-17 08:02:42.140: E/AndroidRuntime(10317): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-17 08:02:42.140: E/AndroidRuntime(10317): at com.Novusmedia.Transact.UserFormActivity$MyAsyncTask.doInBackground(UserFormActivity.java:64)
01-17 08:02:42.140: E/AndroidRuntime(10317): at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-17 08:02:42.140: E/AndroidRuntime(10317): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
这是我的代码:
TextView txtErrorMsg;
private class MyAsyncTask extends AsyncTask<String, String, JSONObject> {
JSONObject json;
@Override
protected JSONObject doInBackground(String... params) {
String host = "my_url";
GamerFunctions gamerFunction = new GamerFunctions();
HttpParams httpParams = new BasicHttpParams();
int timeout = 100;
HttpConnectionParams.setConnectionTimeout(httpParams, timeout);
HttpConnectionParams.setSoTimeout(httpParams, timeout);
HttpClient client = new DefaultHttpClient(httpParams);
try {
HttpGet request = new HttpGet(new URI(host));
HttpResponse response = client.execute(request);
StatusLine status = response.getStatusLine();
if (status.getStatusCode() == HttpStatus.SC_OK) {
if (params.length != 3)
return null;
json = gamerFunction.addNewGamer(params[0], params[1],
params[2]);
} else {
// update ui thread
}
} catch (ClientProtocolException e) {
e.printStackTrace();
// txtErrorMsg.setText("timeout!!!");
} catch (IOException e) {
e.printStackTrace();
// txtErrorMsg.setText("timeout!!!");
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return json;
}
}
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()
&& cm.getActiveNetworkInfo().isAvailable()
&& cm.getActiveNetworkInfo().isConnected()) {
return true;
}
return false;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_form);
final boolean isOnline = isOnline();
ImageButton btnPlay = (ImageButton) findViewById(R.id.btnSubmit);
final EditText txtName = (EditText) findViewById(R.id.txtName);
final EditText txtPhone = (EditText) findViewById(R.id.txtContact);
final EditText txtpharmacy = (EditText) findViewById(R.id.txtPharmacy);
txtErrorMsg = (TextView) findViewById(R.id.txtErromsg);
btnPlay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String name = txtName.getText().toString();
String phone = txtPhone.getText().toString();
String pharmacy = txtpharmacy.getText().toString();
if (name.isEmpty() || phone.isEmpty() || pharmacy.isEmpty()) {
txtErrorMsg.setText("All fields are mandatory");
} else {
if (phone.length() != 10) {
txtErrorMsg.setText("Phone must be 10 characters");
} else {
if (isOnline) {
txtErrorMsg.setTextColor(Color
.parseColor("#006600"));
txtErrorMsg.setText("wait game to load......");
new MyAsyncTask().execute(name, phone, pharmacy);
// startActivity(new Intent(UserFormActivity.this,
// GameActivity.class));
} else {
txtErrorMsg
.setText("please check your internet settings!!!");
}
}
}
}
});
}
答案 0 :(得分:0)
答案 1 :(得分:0)
来自日志的这一行说url是不合适的。请检查是否添加了“http://”目标主机不能为空,或者在参数中设置。 scheme = null,host = null,path = www.novusmedia.co.za
答案 2 :(得分:0)
@Override
protected JSONObject doInBackground(String... params) {
//replace your request url from the "my_url"
String host = "my_url";// <= here
GamerFunctions gamerFunction = new GamerFunctions();
HttpParams httpParams = new BasicHttpParams();
快乐编码