这是我的课:
public class LoginTask extends AsyncTask<Void, Void, Void>
{
Context c;
MainActivity main;
String profileid;
@Override
protected void onPreExecute() {
}
@Override
protected Void doInBackground(Void... unused) {
try {
String semail = MainActivity.email.getText().toString();
String spassword = MainActivity.password.getText().toString();
String savedresponse;
String postURL = "http://(My URL)";
HttpPost post = new HttpPost(postURL);
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 5000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient client = new DefaultHttpClient(httpParameters);
post.setHeader("Content-type","application/json");
JSONObject jsonobj = new JSONObject();
jsonobj.put("username", semail);
jsonobj.put("password", spassword);
StringEntity se = new StringEntity(jsonobj.toString());
post.setEntity(se);
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
savedresponse = EntityUtils.toString(resEntity);
if (resEntity != null)
{
Log.i("RESPONSE",savedresponse);
JSONObject json = new JSONObject(savedresponse);
int responseCode;
responseCode = json.getInt("ResponseCode");
profileid = json.getString("ProfileID");
SharedPreferences prefs = main.getSharedPreferences("ProfileName", Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.clear();
editor.putString("ProfileCookie", profileid);
editor.commit();
if(responseCode == 100) {
MainActivity.email.setText("");
MainActivity.password.setText("");
main.Login();
}
else
{
Toast.makeText(c, "Username or Password wrong", Toast.LENGTH_SHORT).show();
}
}
if (resEntity == null)
{
Log.i("NO RESPONSE",EntityUtils.toString(resEntity));
Toast.makeText(c, "NO RESPONSE", Toast.LENGTH_SHORT).show();
}
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
}
}
这是我在错误日志中得到的:
10-04 02:30:07.283: W/System.err(1015): java.lang.NullPointerException
10-04 02:30:07.283: W/System.err(1015): at com.example.testproject.LoginTask.doInBackground(LoginTask.java:88)
10-04 02:30:07.283: W/System.err(1015): at com.example.testproject.LoginTask.doInBackground(LoginTask.java:1)
10-04 02:30:07.283: W/System.err(1015): at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-04 02:30:07.283: W/System.err(1015): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-04 02:30:07.283: W/System.err(1015): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-04 02:30:07.283: W/System.err(1015): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-04 02:30:07.294: W/System.err(1015): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-04 02:30:07.294: W/System.err(1015): at java.lang.Thread.run(Thread.java:841)
当我调试时,我发现应用程序在共享首选项行时崩溃,我从过去两天被困在这里并且无法找到任何解决方案
请帮助我更正我的代码。如果可能,请发布更正的代码。