我正在使用http客户端学习连接服务器,但是当我是请求数据时,通常会失败并超时,如何解决?
这是我的代码
public void detailURL(String url) {
Log.v("Android Spinner JSON Data Activity", url);
mTask=new Detail().execute(url);
}
private class Detail extends AsyncTask<String, Void, String> {
private static final int REGISTRATION_TIMEOUT = 3 * 1000;
private static final int WAIT_TIMEOUT = 30 * 1000;
private final HttpClient httpclient = new DefaultHttpClient();
final HttpParams params = httpclient.getParams();
HttpResponse response;
private String content = null;
private boolean error = false;
private ProgressDialog dialog = new ProgressDialog(ProfileActivity.this);
protected void onPreExecute() {
dialog.setMessage("Getting your data... Please wait...");
}
protected String doInBackground(String... urls) {
String URL = null;
try {
URL = urls[0];
HttpConnectionParams.setConnectionTimeout(params, REGISTRATION_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, WAIT_TIMEOUT);
ConnManagerParams.setTimeout(params, WAIT_TIMEOUT);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
ByteArrayBody bab = new ByteArrayBody(data,file_name);
HttpPost httpPost = new HttpPost(URL);
//add name value pair for the country code
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("foto", bab);
reqEntity.addPart("tag", new StringBody(register_tag));
reqEntity.addPart("name", new StringBody(name));
reqEntity.addPart("email", new StringBody(email));
reqEntity.addPart("namalengkap", new StringBody(namalengkap));
reqEntity.addPart("telepon", new StringBody(telepon));
reqEntity.addPart("val_lokasi_alamat", new StringBody(val_lokasi_alamat));
httpPost.setEntity(reqEntity);
response = httpclient.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
content = out.toString();
} else{
//Closes the connection.
Log.w("HTTP1:",statusLine.getReasonPhrase());
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
Log.w("HTTP2:",e );
content = e.getMessage();
error = true;
cancel(true);
} catch (IOException e) {
Log.w("HTTP3:",e );
content = e.getMessage();
error = true;
cancel(true);
} catch (Exception e) {
Log.w("HTTP4:",e );
content = e.getMessage();
error = true;
cancel(true);
}
return content;
}
protected void onCancelled() {
mTask.cancel(true);
btn_enable();
ProgressLoadStartRegister.setVisibility(View.GONE);
statusKoneksi();
}
protected void onPostExecute(String content) {
if (error) {
mTask.cancel(true);
ProgressLoadStartRegister.setVisibility(View.GONE);
btn_enable();
statusKoneksi();
// getUpdate();
} else {
ProgressLoadStartRegister.setVisibility(View.GONE);
displaystatis_kontenDetail(content);
mTask.cancel(true);
btn_enable();
}
}
}
private void displaystatis_kontenDetail(String response){
JSONObject json = null;
try {
json = new JSONObject(response);
Log.d("json", json.toString());
if (json.getString(KEY_SUCCESS) != null) {
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
GCMRegistrar.setRegisteredOnServer(getApplicationContext(), true);
// user successfully registred
// Store user details in SQLite Database
Intent LoginActivity = new Intent(getApplicationContext(), LoginActivity.class);
// Close all views before launching Dashboard
LoginActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(LoginActivity);
// Close Registration Screen
finish();
Toast.makeText(getApplicationContext(), "Silakan cek kotak masuk atau spam email anda untuk konfirmasi keaktifan akun",Toast.LENGTH_LONG).show();
} else {
// Error in registration
Toast.makeText(getApplicationContext(), "Username atau Email sudah terdaftar",Toast.LENGTH_SHORT).show();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private void statusKoneksi(){
statusKoneksiRegister.setVisibility(View.VISIBLE);
statusKoneksiRegister.startAnimation(animstatuskoneksi);
handler = new Handler();
handler.postDelayed(new Runnable(){
@Override
public void run(){
statusKoneksiRegister.setVisibility(View.GONE);
statusKoneksiRegister.clearAnimation();
}
}, 4500);
}
usualy error = true;
protected void onPostExecute(String content) {
if (error) {
mTask.cancel(true);
ProgressLoadStartRegister.setVisibility(View.GONE);
btn_enable();
statusKoneksi();
//getUpdate();
} else {
ProgressLoadStartRegister.setVisibility(View.GONE);
displaystatis_kontenDetail(content);
mTask.cancel(true);
btn_enable();
}
}
我的清单中的这种渗透
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- GCM connects to Internet Services. -->
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- Creates a custom permission so only this app can receive its messages. -->
<permission
android:name="com.droidersuin.project.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.droidersuin.project.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- Network State Permissions to detect Internet status -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Permission to vibrate -->
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<permission
android:name="com.droidersuin.project.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.droidersuin.project.permission.MAPS_RECEIVE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS"/>
<uses-permission android:name="android.permission.CAMERA"/>
对不起我的英文,感谢您的参与者......:)