我開始爲Android Studio的應用程序執行同步。我的代碼是:異步任務和敬酒 - Android
private class Check_Loguin_Request extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... strings) {
//Declaration of variables
String User = strings[0];
String Pass = strings[1];
DefaultHttpClient httpClient;
HttpPost Request = new HttpPost(url_Loguin);
HttpResponse Response;
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
// The default value is zero, that means the timeout is not used.
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
httpClient = new DefaultHttpClient(httpParameters);
List<NameValuePair> BodyRequest_Elements = new ArrayList<NameValuePair>();
BodyRequest_Elements.add(new BasicNameValuePair("user_name", User));
BodyRequest_Elements.add(new BasicNameValuePair("user_passwd", Pass));
try {
HttpEntity entity = new UrlEncodedFormEntity(BodyRequest_Elements);
Request.setHeader(entity.getContentType());
Request.setEntity(entity);
Response = httpClient.execute(Request);
HttpEntity entity2 = Response.getEntity();
InputStream is = entity2.getContent();
return Response.toString();
}
catch (Exception ex){
Log.getStackTraceString(ex);
return null;
}
}
protected void onPostExecute(String result){
Toast.makeText(this, "Task Finalized: " + result, Toast.LENGTH_SHORT).show();
}
}
這個類是一個外部類我的主要活動(Sync_Class)(Loguin_Activity),當我把這個在敬酒IDE給我的錯誤。那我怎麼能從我的活動中發送上下文?提前致謝,並對我的英語感到抱歉!
PD1:如果您需要更多的代碼或信息,請給我建議! :d
Check_Loguin_Request類是否在Loguin_Activity中? – 2014-11-20 18:41:03