|
|
|
ลิ้ง JSON จาก URL ไม่อ่านค่าออกมาเป็น Array แบบ1 มิติครับ แก้Code ตรงไหนแนะนำที่ครับ |
|
|
|
|
|
|
|
ผมทำ JSON แต่ไม่โชว์ อ่ะครับ
Code (Android-Java)
ArrayList<HashMap<String, String>> contactList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactList = new ArrayList<>();
lv = (ListView)findViewById(R.id.listView);
new GetContacts().execute();
}
private class GetContacts extends AsyncTask<Void, Void, Void>{
@Override
protected void onPreExecute() {
super.onPreExecute();
//show loading dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... voids) {
HttpHandler sh = new HttpHandler();
String jsonSTr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url:" +jsonSTr);
if (jsonSTr != null){
try {
JSONObject jsonObject = new JSONObject(jsonSTr);
JSONArray contacts = jsonObject.getJSONArray("1");
for (int i = 0; i<contacts.length(); i++){
JSONObject c = contacts.getJSONObject(i);
String pr_id = c.getString("pr_id");
//String id = c.getString("id");
//String name = c.getString("name");
//String email = c.getString("email");
//String address = c.getString("address");
//String gender = c.getString("gender");
//JSONObject phone = c.getJSONObject("phone");
//String mobile = phone.getString("mobile");
//String home = phone.getString("home");
//String office = phone.getString("office");
HashMap<String, String> contact = new HashMap<>();
contact.put("pr_id", pr_id);
contactList.add(contact);
}
}catch (final JSONException e){
Log.e(TAG, "JSON parsing error:" +e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,
"JSON parsing error:" +e.getMessage(),
Toast.LENGTH_LONG).show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,
"Couldn't get json from server.",
Toast.LENGTH_SHORT).show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if (pDialog.isShowing()){
pDialog.dismiss();
}
ListAdapter adapter = new SimpleAdapter(
MainActivity.this,contactList,
R.layout.list_item, new String[]{"pr_id", "email", "mobile"},
new int[] {R.id.name, R.id.email, R.id.mobile});
lv.setAdapter(adapter);
}
}
}
JSON ผม
{"1":{
"pr_id":"00000001",
"pr_brand":"0",
"pr_name":"\u0e40\u0e17\u0e2a",
"pr_cat":"2",
"pr_color":"",
"pr_qty":"3000",
"pr_tmp":"33",
"pr_des":"test",
},
"2":{
"pr_id":"00000002",
"pr_brand":"2",
"pr_name":"CAR MAX BOX",
"pr_cat":"2",
"pr_color":"",
"pr_qty":"5000",
"pr_tmp":"3279",
"pr_des":"DENGO CAR MAX BOX",
}
}
ใครทราบช่วยผมแก้ที่ครับ พึ่งหัดทำครับ
Tag : Mobile, JavaScript, Android, JAVA
|
|
|
|
|
|
Date :
2017-08-21 23:45:10 |
By :
sinxii13 |
View :
1198 |
Reply :
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JSON ผมเป็นแบบไม่มี [] ผมเลย งง อ่ะครับ มีวิธีแก้code ให้ออ่านค่า ออกมาได้ไหมครับ
{"1":{
"pr_id":"00000001",
"pr_brand":"0",
"pr_name":"\u0e40\u0e17\u0e2a",
"pr_cat":"2",
"pr_color":"",
"pr_qty":"3000",
"pr_tmp":"33",
"pr_des":"test",
},
"2":{
"pr_id":"00000002",
"pr_brand":"2",
"pr_name":"CAR MAX BOX",
"pr_cat":"2",
"pr_color":"",
"pr_qty":"5000",
"pr_tmp":"3279",
"pr_des":"DENGO CAR MAX BOX",
}
}
|
|
|
|
|
Date :
2017-08-22 15:21:13 |
By :
sinxii13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class MainActivity extends AppCompatActivity {
private ListView jsonListview;
private ArrayList<String> exData;
private ProgressDialog pDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
jsonListview = (ListView)findViewById(R.id.json_listview);
exData = new ArrayList<String>();
//exData.add("Test1");
//exData.add("Test2");
new AsyncTask<Void, Void, Void>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
URL url = new URL("//url");
URLConnection urlConnection = url.openConnection();
HttpsURLConnection httpsURLConnection =(HttpsURLConnection)urlConnection;
httpsURLConnection.setAllowUserInteraction(false);
httpsURLConnection.setInstanceFollowRedirects(true);
httpsURLConnection.setRequestMethod("GET");
httpsURLConnection.connect();
InputStream inputStream = null;
if (httpsURLConnection.getResponseCode() == HttpsURLConnection.HTTP_OK)
inputStream = httpsURLConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"),8);
StringBuilder stringBuilder = new StringBuilder();
String line = null;
while ((line=reader.readLine()) !=null){
stringBuilder.append(line+ "\n");
}
inputStream.close();
Log.d("JSON Result", stringBuilder.toString());
JSONObject jsonObject = new JSONObject(stringBuilder.toString());
for (int i=0; i < jsonObject.length(); i++){
exData.add(jsonObject.getString("1"));
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1, exData);
jsonListview.setAdapter(myAdapter);
pDialog.dismiss();
}
}.execute();
}
}
อันนี้ผมลองเขียนอีกแบบ ได้ค่าออกมา แต่ยกมาทั้งหมดเลยอ่ะครับ
แบบเป็นก้อนๆเลย
|
|
|
|
|
Date :
2017-08-22 15:23:59 |
By :
sinxii13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ตอบความคิดเห็นที่ : 1 เขียนโดย : mr.win เมื่อวันที่ 2017-08-22 14:35:15
รายละเอียดของการตอบ ::
ผมไปขอ JSON มาจากโปรแกรมเมอร์ที่ออฟฟิตมาเขียนแอฟอ่ะครับ แล้วเค้าให้มาเป็นแบบนี้
{"1":{
"pr_id":"00000001",
"pr_brand":"0",
"pr_name":"\u0e40\u0e17\u0e2a",
"pr_cat":"2",
"pr_color":"",
"pr_qty":"3000",
"pr_tmp":"33",
"pr_des":"test",
},
"2":{
"pr_id":"00000002",
"pr_brand":"2",
"pr_name":"CAR MAX BOX",
"pr_cat":"2",
"pr_color":"",
"pr_qty":"5000",
"pr_tmp":"3279",
"pr_des":"DENGO CAR MAX BOX",
}
}
|
|
|
|
|
Date :
2017-08-23 19:40:32 |
By :
sinxii13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|