-1
我停留在那個一直給我一個鑄造錯誤此HTTP請求的文件。我是新來登錄在網絡上,所以我也跟着說已經過時代碼的HttpParams進口的教程,我在這裏找到一個解決方案要求我導入和包括來自cz.msebera.android.httpclient
依賴作爲片段婁代碼,但現在我得到一個鑄造錯誤我不明白。任何幫助,這將不勝感激。我的Android應用程序是用戶登錄和註冊Android的Http請求鑄造錯誤
package com.geeky.systems.personal.als;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import org.json.JSONObject;
import java.util.ArrayList;
import cz.msebera.android.httpclient.HttpEntity;
import cz.msebera.android.httpclient.HttpResponse;
import cz.msebera.android.httpclient.NameValuePair;
import cz.msebera.android.httpclient.client.HttpClient;
import cz.msebera.android.httpclient.client.entity.UrlEncodedFormEntity;
import cz.msebera.android.httpclient.client.methods.HttpPost;
import cz.msebera.android.httpclient.conn.ClientConnectionManager;
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient;
import cz.msebera.android.httpclient.message.BasicNameValuePair;
import cz.msebera.android.httpclient.params.BasicHttpParams;
import cz.msebera.android.httpclient.params.HttpConnectionParams;
import cz.msebera.android.httpclient.params.HttpParams;
import cz.msebera.android.httpclient.util.EntityUtils;
//import org.apache.http.params.HttpConnectionParams;
//import org.apache.http.params.HttpParams;
public class ServerRequests {
ProgressDialog progressDialog;
public final static int CONNECTION_TIMEOUT = 1000 * 20;
public final static String SERVER_ADDRESS = "http://upishi.net/";
public ServerRequests(Context context){
progressDialog = new ProgressDialog(context);
progressDialog.setCancelable(false);
progressDialog.setTitle("Processing");
progressDialog.setMessage("Please wait...");
}
public void storeUserDataInBackground(User user, GetUserCallback userCallback){
progressDialog.show();
new storeUserDataAsyncTask(user, userCallback).execute();
}
public void fetchUserDataInBackground(User user, GetUserCallback callBack){
progressDialog.show();
new fetchUserDataAsyncTask(user, callBack).execute();
}
public class storeUserDataAsyncTask extends AsyncTask<Void, Void, Void>{
User user;
GetUserCallback userCallback;
public storeUserDataAsyncTask(User user, GetUserCallback userCallback){
this.user = user;
this.userCallback = userCallback;
}
@Override
protected Void doInBackground(Void... params) {
ArrayList<NameValuePair> dataToSend = new ArrayList<NameValuePair>();
dataToSend.add(new BasicNameValuePair("name", user.name));
dataToSend.add(new BasicNameValuePair("username", user.username));
dataToSend.add(new BasicNameValuePair("password", user.password));
dataToSend.add(new BasicNameValuePair("occupation", user.occupation));
dataToSend.add(new BasicNameValuePair("gender", user.gender));
dataToSend.add(new BasicNameValuePair("email", user.email));
dataToSend.add(new BasicNameValuePair("age", user.age + ""));
HttpParams httpRequestParams = (HttpParams) new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpClient client = new DefaultHttpClient((ClientConnectionManager) httpRequestParams);
HttpPost post = new HttpPost(SERVER_ADDRESS + "Register.php");
try{
post.setEntity(new UrlEncodedFormEntity(dataToSend));
client.execute(post);
}
catch (Exception e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
progressDialog.dismiss();
userCallback.done(null);
super.onPostExecute(aVoid);
}
}
public class fetchUserDataAsyncTask extends AsyncTask<Void, Void, User> {
User user;
GetUserCallback userCallback;
public fetchUserDataAsyncTask(User user, GetUserCallback userCallback) {
this.user = user;
this.userCallback = userCallback;
}
@Override
protected User doInBackground(Void... params) {
ArrayList<NameValuePair> dataToSend = new ArrayList<NameValuePair>();
dataToSend.add(new BasicNameValuePair("username", user.username));
dataToSend.add(new BasicNameValuePair("password", user.password));
HttpParams httpRequestParams = (HttpParams) new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpClient client = new DefaultHttpClient((ClientConnectionManager) httpRequestParams);
HttpPost post = new HttpPost(SERVER_ADDRESS + "FetchUserData.php");
User returnedUser = null;
try{
post.setEntity(new UrlEncodedFormEntity(dataToSend));
HttpResponse httpResponse = client.execute(post);
HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
JSONObject jObject = new JSONObject(result);
if (jObject.length()==0){
returnedUser = null;
}else{
String name = jObject.getString("name");
String occupation = jObject.getString("occupation");
String gender = jObject.getString("gender");
String email = jObject.getString("email");
int age = jObject.getInt("age");
returnedUser = new User(name,user.username,user.password,occupation,gender,email,age);
}
}
catch (Exception e){
e.printStackTrace();
}
return returnedUser;
}
@Override
protected void onPostExecute(User returnedUser) {
progressDialog.dismiss();
userCallback.done(returnedUser);
super.onPostExecute(returnedUser);
}
}
}
的錯誤我得到這個樣子的
FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.ClassCastException: cz.msebera.android.httpclient.params.BasicHttpParams cannot be cast to cz.msebera.android.httpclient.conn.ClientConnectionManager
at com.geeky.systems.personal.als.ServerRequests$storeUserDataAsyncTask.doInBackground(ServerRequests.java:80)
at com.geeky.systems.personal.als.ServerRequests$storeUserDataAsyncTask.doInBackground(ServerRequests.java:54)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
當問問題,請格式化爲最大的可讀性(感謝@ martijnn2008)代碼/日誌,請從乞求不要緊迫性 - 它對**志願者**來說並不緊急。 – halfer