這是我的JSON解析器類JSON解析錯誤(追加結果)
public class JSONParser {
String charset = "UTF-8";
HttpURLConnection conn;
DataOutputStream wr;
StringBuilder result = new StringBuilder();
URL urlObj;
JSONObject jObj = null;
StringBuilder sbParams;
String paramsString;
public JSONObject makeHttpRequest(String url, String method,
HashMap<String, String> params) {
sbParams = new StringBuilder();
int i = 0;
for (String key : params.keySet()) {
try {
if (i != 0){
sbParams.append("&");
}
sbParams.append(key).append("=")
.append(URLEncoder.encode(params.get(key), charset));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
i++;
}
if (method.equals("POST")) {
// request method is POST
try {
urlObj = new URL(url);
conn = (HttpURLConnection) urlObj.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept-Charset", charset);
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.connect();
paramsString = sbParams.toString();
wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(paramsString);
wr.flush();
wr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
else if(method.equals("GET")){
// request method is GET
if (sbParams.length() != 0) {
url += "?" + sbParams.toString();
}
try {
urlObj = new URL(url);
conn = (HttpURLConnection) urlObj.openConnection();
conn.setDoOutput(false);
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept-Charset", charset);
conn.setConnectTimeout(15000);
conn.connect();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
//Receive the response from the server
InputStream in = new BufferedInputStream(conn.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
Log.d("JSON Parser", "result: " + result.toString());
} catch (IOException e) {
e.printStackTrace();
}
conn.disconnect();
// try parse the string to a JSON object
try {
jObj = new JSONObject(result.toString());
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON Object
return jObj;
}}
的AsyncTask
class CreateNewProduct extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
mView=new CatLoadingView();
mView.show(getSupportFragmentManager(),"");
}
protected String doInBackground(String... args) {
int success;
String name = inputName.getText().toString();
String password = inputPassword.getText().toString();
try {
// Building Parameters
HashMap<String,String> h1=new HashMap<>();
h1.put("username", name);
h1.put("password", password);
// getting JSON Object
//url accepts POST method
JSONObject json= jsonParser.makeHttpRequest(api_url,"POST", h1);
// check log cat from response
Log.e("Create Response 1", json.toString());
JsonData=json.toString();
JSONObject reader=new JSONObject(JsonData);
JSONObject jobj=reader.getJSONObject("user");
Log.e("Create Response 2", jobj.toString());
JSONObject jobj2 = jobj.getJSONObject("data");
String user_id = jobj2.getString("USER_NAME");
//String bimage=jobj2.getString("DISPLAY_PIC");
Intent intent = new Intent(getBaseContext(), MainActivity.class);
intent.putExtra("id", user_id);
// intent.putExtra("image",bimage);
startActivity(intent);
} catch (JSONException e) {
e.printStackTrace();
}
// check for success tag
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
mView.dismiss();
}
1:當我輸入正確的登錄憑據登錄會成功,而
JSONResult is
{"user":{"data":{"USER_ID":"1","USER_NAME":"Vaisakh CV","BRANCH_ID":"1"}}} .
2:但是,當我輸入錯誤的憑據的JSONResult是
{"user":{"error":"Invalid data!"}}
3:我的問題是後輸入正確的憑據錯誤嘗試JSONResult是
{"user":{"error":"Invalid data!"}}{"user":{"data":{"USER_ID":"1","USER_NAME":"Vaisakh CV","BRANCH_ID":"1"}}}
我需要後只有
{"user":{"data":{"USER_ID":"1","USER_NAME":"Vaisakh CV","BRANCH_ID":"1"}}}
如何爲r解決這個問題?提前致謝!
哇!你給我我想要的!謝謝...! :)字符串追加是我的主要問題..!你讓我今天一整天都感覺很好!再一次感謝你! –