从服务器获取数据时,AsyncTask不起作用

时间:2014-03-21 12:51:25

标签: android android-asynctask android-json

我正在研究一个项目..当我在2.2版本中运行我的项目时,我的工作方式很好....但是当我在我的代码中使用AsyncTask并在4.0版本上运行avd时它无法正常工作。 ..我在logcat中发现了以下错误

  1. 解析数据时出错java.net.SocketException:Socket closed
  2. 解析数据时出错org.json.JSONException:客户没有值 我可以在我的代码中正确实现AsyncTask吗? 请检查我的代码......这会出现什么问题....

             public class Login extends Activity {
     String success, cus_id, cus_name;
          SessionManager session;
    
         @Override
          protected void onCreate(Bundle savedInstanceState) {
    
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);
    
    Button submit = (Button) findViewById(R.id.loginbutton);
    
    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // TODO Auto-generated method stub
            com.amplio.MyCustomEditText et = (com.amplio.MyCustomEditText) findViewById(R.id.etmob);
            String mobileno = et.getText().toString();
            if (mobileno.length() > 0) {
                final ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
                postParameters.add(new BasicNameValuePair("cus_mob",
                        mobileno));
    
                Thread thread = new Thread() {
                    @Override
                    public void run() {
                        try {
                            String response = null;
                            response = LoginHttpClient
                                    .executeHttpPost(
                                            "http://10.0.2.2/android/mobile_no.php",
                                            postParameters);
                            System.out.println(response);
                            JSONObject json = new JSONObject(response);
                            JSONArray jArray = json
                                    .getJSONArray("customer");
                            for (int i = 0; i < jArray.length(); i++) {
                                JSONObject json_data = jArray
                                        .getJSONObject(i);
                                success = json_data.getString("success");
                                cus_id = json_data.getString("cus_id");
                                cus_name = json_data.getString("cus_name");
                            }
                            if (success.equals("1")) {
                                session = new SessionManager(
                                        getApplicationContext());
                                session.createLoginSessionRemMe(cus_id,
                                        cus_name);
                                Intent iv = new Intent(
                                        getApplicationContext(),
                                        Verify.class);
                                startActivity(iv);
                            } else {
                                // not valid email id or pass
                                Toast.makeText(getApplicationContext(),
                                        "Incorrect Mobile No",
                                        Toast.LENGTH_LONG).show();
                            }
                        } catch (Exception e) {
                        }
                    }
                };
                thread.start();
    
            }
    
            else {
                // display message if text fields are empty
                Toast.makeText(getBaseContext(), "Mobile no is required",
                        Toast.LENGTH_SHORT).show();
            }
        }
    });
           }}
    

0 个答案:

没有答案