2013-06-23 60 views
0

我想在android中進行註冊過程。但是,當我填寫的領域,並點擊我的註冊按鈕「不幸崩潰」發生。在logcat中我發現,JSONPerser.java中的httpClient.execute(httpPost)不起作用。發送和接收來自服務器不工作的響應android

誰能plz幫助....

Register.java

register.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       String fNameVal = fname.getText().toString(); 
       String lNameVal = lname.getText().toString(); 
       String emailValue = email.getText().toString(); 
       String passwordValue = password.getText().toString(); 

       UserFunctions userFunction = new UserFunctions(); 
       JSONObject json = userFunction.registerUser(fNameVal, lNameVal, emailValue, passwordValue); 

       try { 



        if (json.getString(KEY_SUCCESS) != null) { 
         /*registerErrorMsg.setText("");*/ 
         String res = json.getString(KEY_SUCCESS); 
         if(Integer.parseInt(res) == 1){ 
          // user successfully registred 
          // Store user details in SQLite Database 
          DatabaseHandler db = new DatabaseHandler(getApplicationContext()); 
          JSONObject json_user = json.getJSONObject("user"); 

          // Clear all previous data in database 
          userFunction.logoutUser(getApplicationContext()); 
          db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_CREATED_AT));       
          // Launch Dashboard Screen 
          Toast.makeText(getApplicationContext(), "Registration Successful. Please Login.", Toast.LENGTH_LONG).show(); 
          Intent dashboard = new Intent(getApplicationContext(), Login.class); 
          // Close all views before launching Dashboard 
          dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
          startActivity(dashboard); 
          // Close Registration Screen 
          finish(); 
         }else{ 
          // Error in registration 
          /*registerErrorMsg.setText("Error occured in registration");*/ 
          Toast.makeText(getApplicationContext(), "Error occured in registration", Toast.LENGTH_LONG).show(); 
         } 
        } 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 

UserFunctions.java

public JSONObject registerUser(String firstname, String lastname, String email, String password){ 
     // Building Parameters 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     params.add(new BasicNameValuePair("tag", register_tag)); 
     params.add(new BasicNameValuePair("firstName", firstname)); 
     params.add(new BasicNameValuePair("lastName", lastname)); 
     params.add(new BasicNameValuePair("email", email)); 
     params.add(new BasicNameValuePair("password", password)); 

     System.out.println(firstname + lastname + email + password + registerURL); 

     // getting JSON Object 
     JSONObject json = jsonParser.getJSONFromUrl(registerURL, params); 
     // return json 
     return json; 
    } 

JSONParser.java

公衆的JSONObject getJSONFromUrl(字符串URL,列表PARAMS){

// Making HTTP request 
    try { 
     // defaultHttpClient 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 
     System.out.println("HTTP POST: " + httpPost); 
     httpPost.setEntity(new UrlEncodedFormEntity(params)); 

     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(); 
     Log.e("JSON", json); 
    } 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; 

} 
+0

可能是主線程異常的網絡,請對NetworkOnMainThread異常和AsyncTask實現做一些研究。 –

回答

2

getJSONFromUrl

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
StrictMode.setThreadPolicy(policy); 

而且在同一個函數

while ((line = reader.readLine()) != null) { 
    sb.append(line + "n"); 
} 

更改它添加此行

while ((line = reader.readLine()) != null) {     
    sb.append(line + "\n"); 
} 
+0

嘿!這太棒了......現在工作得很好!非常感謝。 – Mehdi

相關問題