2016-02-29 134 views
0

enter image description here給出錯誤在EXCEPTION .... json數據解析錯誤在catch在postexecute metod請幫助如何解決。當數據輸入它,它開始工作smothly但時間到發送數據也顯示,JSON數據解析錯誤json數據解析錯誤android studio

/** 
* Created by Mian on 2/28/2016. 
*/ 
import android.content.Context; 
import android.os.AsyncTask; 
import android.widget.Toast; 
import org.json.JSONException; 
import org.json.JSONObject; 
import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.net.URLEncoder; 
public class SignupActivity extends AsyncTask<String, Void, String> { 
    private Context context; 
    public SignupActivity(Context context) { 
     this.context = context; 
    } 

    protected void onPreExecute() { 
    } 
    @Override 
    protected String doInBackground(String... arg0) { 
     String fullName = arg0[0]; 
     String userName = arg0[1]; 
     String passWord = arg0[2]; 
     String phoneNumber = arg0[3]; 
     String emailAddress = arg0[4]; 
     String link; 
     String data; 
     BufferedReader bufferedReader; 
     String result; 
     try { 
      data = "?fullname=" + URLEncoder.encode(fullName, "UTF-8"); 
      data += "&username=" + URLEncoder.encode(userName, "UTF-8"); 
      data += "&password=" + URLEncoder.encode(passWord, "UTF-8"); 
      data += "&phonenumber=" + URLEncoder.encode(phoneNumber, "UTF-8"); 
      data += "&emailaddress=" + URLEncoder.encode(emailAddress, "UTF-8"); 
      link = "http://livethuglife.com/signup.php" + data; 
      URL url = new URL(link); 
      HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
      bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream())); 
      result = bufferedReader.readLine(); 
      return result; 
     } catch (Exception e) { 
      return new String("Exception: " + e.getMessage()); 
     } 
    } 
    @Override 
    protected void onPostExecute(String result) { 
     String jsonStr = result.toString(); 
     if (jsonStr != null) { 
      try { 
       JSONObject jsonObj = new JSONObject(jsonStr); 
       String query_result = jsonObj.getString("query_result"); 
       if (query_result.equals("SUCCESS")) { 
        Toast.makeText(context, "Data inserted successfully. Signup successfull.", Toast.LENGTH_SHORT).show(); 
       } else if (query_result.equals("FAILURE")) { 
        Toast.makeText(context, "Data could not be inserted. Signup failed.", Toast.LENGTH_SHORT).show(); 
       } else { 
        Toast.makeText(context, "Couldn't connect to remote database.", Toast.LENGTH_SHORT).show(); 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
       Toast.makeText(context, "Error parsing JSON data.", Toast.LENGTH_SHORT).show(); 
      } 
     } else { 
      Toast.makeText(context, "Couldn't get any JSON data.", Toast.LENGTH_SHORT).show(); 
     } 
    } 
} 
+0

什麼是錯誤您收到? –

+0

json數據解析錯誤 –

+0

那是你的錯誤,logcat中有什麼錯誤? –

回答

1

更好地使用Class,按您的Form,你已經被髮布到Server

  1. 一個簡單的Java類,帶有需要將完整的表單數據載入服務器的字段數。像我已經定義爲SignUp類。
  2. 然後使該Class SignUpobject填補了object相應要張貼到服務器
  3. 現在改變的是objectJSON使用谷歌gson.jar(你可以下載它通過搜索它在互聯網上)

    一個簡單的例子,我已經向你..

簡單POJOClass註冊

Class SignUP{ 
    private String fullname; 
    private String username; 
    private String passoword; // Define Number of Variable as per your Need 
    private String phonenumber; 
    private String emailaddress; 

     //Getter and Setters Method. 
} 

現在實例化類,並嘗試谷歌GSON的jar包到解析它作爲JSON Object

一個簡單的邏輯ObjectClass註冊轉換爲JSON

protected String doInBackground(String... arg0) { 
    String fullName = arg0[0]; 
    String userName = arg0[1]; 
    String passWord = arg0[2]; 
    String phoneNumber = arg0[3]; 
    String emailAddress = arg0[4]; 

    SignUP obj = new SignUP(); 
    obj.setFullName(URLEncoder.encode(fullName, "UTF-8")); 
    obj.setUserName(URLEncoder.encode(userName, "UTF-8")); 
    obj.setPassword(URLEncoder.encode(passWord, "UTF-8");); 
    obj.setPhoneNumber(URLEncoder.encode(phoneNumber,"UTF-8");); 
    obj.setEmailAddress(URLEncoder.encode(emailAddress, "UTF-8");); 

    Gson gson = new Gson(); 

    System.out.println(gson.toJson(obj)); 
    } 

輸出:

{ 
    "fullName":"FullName", 
    "username":"Lokesh", 
    "password":"Gupta", 
    "phonenumber":"73479273423", 
    "emailaddress":"abc @abc.com" 
} 

你也可以嘗試How to Convert Java Object to JSON Using Google GSON API

+0

請你詳細說明 –

+0

是否適合理解我的想法。如果你有一點java的知識.. 謝謝 –

+0

傳遞一些東西給你做的類對象,那麼我會通過什麼..? –