我想從SQL數據庫的錢包數組中填充我的微調,然後存儲用戶在特定用戶詳細信息中選擇的錢包ID,而不是錢包名稱。我已經寫了這個特定的代碼,並添加了屏幕截圖以及錯誤。如何解析Json數組和Json對象在Android中有兩個鍵和值?
Android部分
public class User extends AppCompatActivity {
ArrayAdapter<String> adapter;
ArrayList<Populate> listItems;
LinkedHashMap<String,String> walletId;
Button logout,editdetails;
SharedPreferences sp;
SharedPreferences.Editor editor;
public static final String DEFAULT = "N/A";
TextView usermail;
String email;
int id;
public static final int DEFAULTI = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
listItems = new ArrayList<>();
usermail = (TextView) findViewById(R.id.usermail);
editdetails = (Button) findViewById(R.id.editdetails);
logout = (Button) findViewById(R.id.logout);
sp = getSharedPreferences("Login", Context.MODE_PRIVATE);
email = sp.getString("email", DEFAULT);
usermail.setText("Welcome " + email);
id = sp.getInt("id",DEFAULTI);
logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/** pref = getSharedPreferences("Login", Context.MODE_PRIVATE);
editor = pref.edit();
editor.putBoolean("loginfirst",false);
editor.commit();**/
Intent i = new Intent(User.this, Login.class);
startActivity(i);
finish();
}
});
}
public void onEdit(View v){
BackgroundTask backgroundTask = new BackgroundTask();
backgroundTask.execute(String.valueOf(id));
}
class BackgroundTask extends AsyncTask<String, Void, String> {
ArrayList<Populate> list;
String add_info_url;
@Override
protected void onPreExecute() {
list=new ArrayList<>();
add_info_url = "http://192.168.2.6/Deal%20Engine/editdetails.php";
}
@Override
protected void onPostExecute(String result) {
listItems.addAll(list);
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
//SharedPreferences sp = getSharedPreferences("Login",Context.MODE_PRIVATE);
Intent i = new Intent(User.this, EditDetails.class);
startActivity(i);
}
@Override
protected String doInBackground(String... args) {
BufferedReader reader = null;
try {
URL url = new URL(add_info_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String dataString = URLEncoder.encode("id", "UTF-8") + "=" + URLEncoder.encode(String.valueOf(id), "UTF-8");
Log.d("id", String.valueOf(id));
bufferedWriter.write(dataString);
Log.d("Result", dataString);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String finalJson = buffer.toString();
Log.d("String", finalJson);
JSONObject parentObject = new JSONObject(finalJson);
JSONArray parentArray = parentObject.getJSONArray("user");
StringBuffer finalBufferedData = new StringBuffer();
for (int i = 0; i < parentArray.length(); i++) {
JSONObject finalObject = parentArray.getJSONObject(i);
int status_code = finalObject.getInt("status_code");
String status_desc = finalObject.getString("status_desc");
String user_id = finalObject.getString("user_id");
int id = finalObject.getInt("id");
String name=finalObject.getString("name");
String pass= finalObject.getString("pass");
String location = finalObject.getString("location");
String cards=finalObject.getString("cards");
String category= finalObject.getString("category");
String wallet = finalObject.getString("wallet");
String operator=finalObject.getString("operator");
String loyaltyProgram= finalObject.getString("loyaltyProgram");
String membership= finalObject.getString("membership");
JSONObject walletArrayObject = new JSONObject(finalObject.getString("wallet_array"));
JSONArray walletArray = walletArrayObject.getJSONArray("wallets");
Log.d("Arraysize",String.valueOf(walletArray));
for(int j =0;j<walletArray.length();j++)
{
JSONObject walletObject = walletArray.getJSONObject(j);
list.add(new Populate(String.valueOf(walletObject.getInt("id")),walletObject.getString("wallet_name")));
}
sp = getSharedPreferences("Login",Context.MODE_PRIVATE);
editor=sp.edit();
editor.putString("name",name);
editor.putString("pass",pass);
editor.putInt("id",id);
editor.putString("location",location);
editor.putString("cards",cards);
editor.putString("category",category);
editor.putString("wallet",wallet);
editor.putString("operator",operator);
editor.putString("wallets",String.valueOf(list));
editor.putString("loyaltyProgram",loyaltyProgram);
editor.putString("membership",membership);
editor.commit();
finalBufferedData.append(status_code + " - " + status_desc + " -" + user_id + " -"+id + " -"+name+ " -"+list +"\n");
}
inputStream.close();
httpURLConnection.disconnect();
Log.d("Result", dataString);
return finalBufferedData.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
}
錯誤我得到
06-29 12:53:08.015 28974-28988/com.example.tanmayjain.twowaycommunication D/String: {"user":[{"status_code":"1","status_desc":"Success","user_id":"482","id":"26","name":"tanmay ","pass":"a006b22f887f7d922bafa4c8186ccafd","location":"Ahmedabad","cards":"ICICI Bank","category":"Platinum","wallet":"","operator":"Vodafone","loyaltyProgram":"Any","membership":"All Time","wallet_array":"{\"wallets\":[{\"1\":[\"Paytm\"],\"2\":[\"Freecharge\"],\"3\":[\"Mobikwik\"],\"4\":[\"PayUmoney\"],\"5\":[\"CitrusCash\"],\"6\":[\"Airtel Money\"],\"7\":[\"Oxigen Wallet\"],\"8\":[\"OLAMoney\"],\"9\":[\"HDFC PayZapp\"],\"10\":[\"Chillr by HDFC\"],\"11\":[\"Pockets by ICICI bank\"],\"12\":[\"JioMoney\"],\"13\":[\"SBI Buddy\"],\"14\":[\"mRupee\"],\"15\":[\"Itzcash\"]}]}"}]}
06-29 12:53:08.015 28974-28988/com.example.tanmayjain.twowaycommunication D/Arraysize: [{"1":["Paytm"],"2":["Freecharge"],"3":["Mobikwik"],"4":["PayUmoney"],"5":["CitrusCash"],"6":["Airtel Money"],"7":["Oxigen Wallet"],"8":["OLAMoney"],"9":["HDFC PayZapp"],"10":["Chillr by HDFC"],"11":["Pockets by ICICI bank"],"12":["JioMoney"],"13":["SBI Buddy"],"14":["mRupee"],"15":["Itzcash"]}]
06-29 12:53:08.016 28974-28988/com.example.tanmayjain.twowaycommunication W/System.err: org.json.JSONException: No value for id
06-29 12:53:08.016 28974-28988/com.example.tanmayjain.twowaycommunication W/System.err: at org.json.JSONObject.get(JSONObject.java:389)
06-29 12:53:08.016 28974-28988/com.example.tanmayjain.twowaycommunication W/System.err: at org.json.JSONObject.getInt(JSONObject.java:478)
06-29 12:53:08.016 28974-28988/com.example.tanmayjain.twowaycommunication W/System.err: at com.example.tanmayjain.twowaycommunication.User$BackgroundTask.doInBackground(User.java:167)
06-29 12:53:08.017 28974-28988/com.example.tanmayjain.twowaycommunication W/System.err: at com.example.tanmayjain.twowaycommunication.User$BackgroundTask.doInBackground(User.java:85)
06-29 12:53:08.017 28974-28988/com.example.tanmayjain.twowaycommunication W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295)
php是不相關的 – danopz
if($ result){ \t \t $ output = array(); \t while($ row = mysqli_fetch_assoc($ result)){ \t \t $ output [$ row [「id」]] [0] = $ row [「wallet_name」]; \t} \t} \t \t $ tempArray [「wallets」] [0] = $ output; \t $ usrArray [「user」] [0] = array(「status_code」=>'1','status_desc'=>'成功','user_id'=>'482','id'=> 「name」=> $ name,「pass」=> $ pass,「location」=> $ location,「cards」=> $ cards,「category」=> $ category,「wallet」=> $ wallet「 「=> $運算符,」loyaltyProgram「=> $ loyaltyProgram,」membership「=> $ membership,」wallet_array「=> json_encode($ tempArray)); echo json_encode($ usrArray); } –