我需要一些帮助来解决这个问题,试图绕过整个异步过程并在后台进行()
我收到此错误消息,需要一些帮助才能使其正常工作。
10-20 14:50:55.826: D/request!(29163): starting 10-20 14:50:55.871: D/ProgressBar(29163): updateDrawableBounds: left = 0 10-20 14:50:55.871: D/ProgressBar(29163): updateDrawableBounds: top = 0 10-20 14:50:55.871: D/ProgressBar(29163): updateDrawableBounds: right = 96 10-20 14:50:55.871: D/ProgressBar(29163): updateDrawableBounds: bottom = 96 10-20 14:50:55.961: W/dalvikvm(29163): threadid=11: thread exiting with uncaught exception (group=0x41f57c08) 10-20 14:50:55.991: D/dalvikvm(29163): GC_FOR_ALLOC freed 231K, 8% free 10077K/10916K, paused 11ms, total 11ms 10-20 14:50:55.991: E/AndroidRuntime(29163): FATAL EXCEPTION: AsyncTask #1 10-20 14:50:55.991: E/AndroidRuntime(29163): Process: com.bobcatservice, PID: 29163 10-20 14:50:55.991: E/AndroidRuntime(29163): java.lang.RuntimeException: An error occured while executing doInBackground() 10-20 14:50:55.991: E/AndroidRuntime(29163): at android.os.AsyncTask$3.done(AsyncTask.java:300) 10-20 14:50:55.991: E/AndroidRuntime(29163): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) 10-20 14:50:55.991: E/AndroidRuntime(29163): at java.util.concurrent.FutureTask.setException(FutureTask.java:222) 10-20 14:50:55.991: E/AndroidRuntime(29163): at java.util.concurrent.FutureTask.run(FutureTask.java:242) 10-20 14:50:55.991: E/AndroidRuntime(29163): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 10-20 14:50:55.991: E/AndroidRuntime(29163): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 10-20 14:50:55.991: E/AndroidRuntime(29163): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 10-20 14:50:55.991: E/AndroidRuntime(29163): at java.lang.Thread.run(Thread.java:841) 10-20 14:50:55.991: E/AndroidRuntime(29163): Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?) 10-20 14:50:55.991: E/AndroidRuntime(29163): at java.net.InetAddress.lookupHostByName(InetAddress.java:418) 10-20 14:50:55.991: E/AndroidRuntime(29163): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 10-20 14:50:55.991: E/AndroidRuntime(29163): at java.net.InetAddress.getAllByName(InetAddress.java:214) 10-20 14:50:55.991: E/AndroidRuntime(29163): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137) 10-20 14:50:55.991: E/AndroidRuntime(29163): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 10-20 14:50:55.991: E/AndroidRuntime(29163): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 10-20 14:50:55.991: E/AndroidRuntime(29163): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 10-20 14:50:55.991: E/AndroidRuntime(29163): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670) 10-20 14:50:55.991: E/AndroidRuntime(29163): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509) 10-20 14:50:55.991: E/AndroidRuntime(29163): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 10-20 14:50:55.991: E/AndroidRuntime(29163): at com.bobcatservice.JSONParser.makeHttpRequest(JSONParser.java:98) 10-20 14:50:55.991: E/AndroidRuntime(29163): at com.bobcatservice.Login$AttemptLogin.doInBackground(Login.java:121) 10-20 14:50:55.991: E/AndroidRuntime(29163): at com.bobcatservice.Login$AttemptLogin.doInBackground(Login.java:1) 10-20 14:50:55.991: E/AndroidRuntime(29163): at android.os.AsyncTask$2.call(AsyncTask.java:288) 10-20 14:50:55.991: E/AndroidRuntime(29163): at java.util.concurrent.FutureTask.run(FutureTask.java:237) 10-20 14:50:55.991: E/AndroidRuntime(29163): ... 4 more
这是源代码
//AsyncTask is a seperate thread than the thread that runs the GUI
//Any type of networking should be done with asynctask.
class AttemptLogin extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
boolean failure = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Attempting login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
String username = user.getText().toString();
String password = pass.getText().toString();
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
Log.d("request!", "starting");
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "POST", params);
// check your log for json response
Log.d("Login attempt", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Login Successful!", json.toString());
Intent i = new Intent(Login.this, Welcome.class);
finish();
startActivity(i);
return json.getString(TAG_MESSAGE);
}else{
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null){
Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
}
这是我的JSONParser类
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(final String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
//try {
//jObj = new JSONObject(json);
//} catch (JSONException e) {
//Log.e("JSON Parser", "Error parsing data " + e.toString());
// }
// return JSON String
return jObj;
}
// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
这是我的manfiest.xml文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bobcatservice"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.persmisson.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.bobcatservice.Login"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.bobcatservice.Welcome"
android:label="@string/app_name" >
</activity>
</application>