0

这是我的课:

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)

当我调试时,我发现应用程序在共享首选项行时崩溃,我从过去两天被困在这里并且无法找到任何解决方案

请帮助我更正我的代码。如果可能,请发布更正的代码。

4

6 回答 6

1

利用:

    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);

代替SharedPreferences prefs = main.getSharedPreferences("ProfileName", Context.MODE_PRIVATE);

于 2013-10-04T06:56:26.367 回答
0

在 doInBackground() 方法中只能处理后台进程。您应该将稍后的代码放在 onPostExecute() 方法中。

于 2013-10-04T07:25:48.843 回答
0

你有这个

 MainActivity main;

但是你在哪里初始化main?即使假设它是一个活动类,您也初始化它是错误的。

您可以从活动类中的 sharepreferences 获取值并将参数传递给 asynctask doInbackground。您还可以在活动类中获取 editext 值并将其传递给 asynctaskdoInbackground

您无法从doInbackground

MainActivity.email.setText("");
MainActivity.password.setText("");
Toast.makeText(c, "NO RESPONSE", Toast.LENGTH_SHORT).show(); 
于 2013-10-04T06:51:00.147 回答
0

您在 doInBackground 方法中进行 UI 操作,记住 doInBackground 方法仅用于后台操作。如果要与 UI 交互,则使用 onPostExecute 方法

消除

                MainActivity.email.setText("");
                MainActivity.password.setText(""); 
             & Toast Messages 

来自 doinBackground 并添加onPostExecute方法

您的

  onPostExecute(){

    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();
    }
于 2013-10-04T06:46:41.423 回答
0

你不能在你的doInBackground方法中做 UI 相关的工作。onCreate()在您的方法中移动以下行

     String semail = MainActivity.email.getText().toString();
    String spassword = MainActivity.password.getText().toString();

在您返回您的请求代码并在您的方法doInBackground中编写以下代码。onPostExecute()

    @Override
    protected void onPostExecute(Void result)  
     { 
         f(result== 100) {
                MainActivity.email.setText("");
                MainActivity.password.setText("");
                main.Login();
           }
            else
            {
                Toast.makeText(c, "Username or Password wrong", Toast.LENGTH_SHORT).show();
            }
      }

此外,您还没有在代码中初始化您MainActivity的代码,而是直接引用它。您应该使用当前活动的上下文,即SharedPreferences prefs = c.getSharedPreferences("ProfileName", Context.MODE_PRIVATE);

将您的上下文定义为Context c="youractivityname.this";

于 2013-10-04T06:54:32.017 回答
0

您可以在后台获取 SharedPreference 实例。问题是您的“主”实例尚未初始化。

SharedPreferences sharedPrefs = getApplicationContext().getSharedPreferences("ProfileName", Context.MODE_PRIVATE);
于 2013-10-04T06:59:31.603 回答