2017-01-08 111 views
-2

我已經裝箱的Web API作爲http://hunttreasure.azurewebsites.net/Default.aspx 它返回傑森數組作爲在Android中如何解析JSON陣列

[{ 
    "latitude": 20.938849, 
    "longitude": 77.781681, 
    "placeName": "SBI_CAMP", 
    "hint": "SBI CAMP Treasure Hint" 
}, { 
    "latitude": 20.938835, 
    "longitude": 77.782726, 
    "placeName": "GARAGE CAMP", 
    "hint": "CAMP GARAGE Treasure Hint" 
}] 

如何我可以分析這個JSON陣列充分利用URL和變量保存的值。

+0

在'Retrofit'從非常基本的http://www.javatpoint.com/android-web-service,然後移動到看一看,'gson'庫 –

+1

開始圖書館,其中@ JohnO'Reilly建議 – Rahul

回答

0
package com.example.its.myapplication; 

import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 

public class MainActivity extends AppCompatActivity { 

    private String finalresult; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     new GetJsonData().execute(); 
    } 

    private class GetJsonData extends AsyncTask<Void, Void, Void> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      // before making http calls 

     } 

     @Override 
     protected Void doInBackground(Void... arg0) { 


      String getUrl = "http://hunttreasure.azurewebsites.net/Default.aspx "; 
      try { 
       URL url; 
       HttpURLConnection urlConnection = null; 
       url = new URL(getUrl); 

       urlConnection = (HttpURLConnection) url.openConnection(); 
       urlConnection.setRequestMethod("GET"); 
       urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0"); 

       StringBuffer response = new StringBuffer(); 
       int responseCode = urlConnection.getResponseCode(); 

       if (responseCode == HttpURLConnection.HTTP_OK) { //success 
        BufferedReader inurl = new BufferedReader(new InputStreamReader(
          urlConnection.getInputStream())); 
        String inputLine; 
        while ((inputLine = inurl.readLine()) != null) { 
         response.append(inputLine); 
        } 
        inurl.close(); 

       } else { 

        Log.i("test", "POST request not worked."); 
       } 

       finalresult = response.toString(); 

      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      super.onPostExecute(result); 
      try { 
       parseJson(finalresult); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 

     public void parseJson(String json) throws JSONException { 

      JSONArray jArr = new JSONArray(json); 

      for (int count = 0; count < jArr.length(); count++) { 
       JSONObject obj = jArr.getJSONObject(count); 
       double latitude= obj.getDouble("latitude"); 
       double longitude= obj.getDouble("longitude"); 
       String placeName= obj.getString("placeName"); 
       String hint = obj.getString("hint"); 
      } 
     } 

    }} 
+0

但兄弟第一如何從url http://hunttreasure.azurewebsites.net/Default.aspx獲取json字符串來解析它 –

0

只是一個樣本

public class Language extends Json { 

    private String mUuid; 
    private String mName; 

    public Language() { } 

    public String getName() { 
     return mName; 
    } 

    public void setName(final String name) { 
     mName = name; 
    } 

    public String getUuid() { 
     return mUuid; 
    } 

    public void setUuid(final String uuid) { 
     mUuid = uuid; 
    } 

    @Override 
    public void init(final String... args) { 
     final String jsonStr = args[0]; //Here we get a json object like a string 

     try { 
      JSONObject json = new JSONObject(jsonStr); //Parse it 
      mUuid = json.getString("Id"); 
      mName = json.getString("Name"); 

      mIsOk = true; 
     } catch (JSONException e) { 
      e.printStackTrace(); 
      mIsOk = false; 
     } 
    } 
} 

對於JSON數組,你應該使用JSONArray而不是JSONObject
ADDED

URL url = new URL(mPath); 
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
connection.setRequestMethod("GET"); 
connection.setConnectTimeout(mTimeout); 
int code = connection.getResponseCode(); 
boolean isOk = HttpRequest.HTTP_OK == code; 

if (isOk) { 
    String response = ConnectionUtils.readToString(connection.getInputStream()); 
} 
+0

首先,我必須獲取JSON字符串,如何從url中獲取json字符串http://hunttreasure.azurewebsites.net/Default.aspx –

+0

它不在url中,它是在請求的主體中。看到一個Java類'HttpUrlConnection'。 –

+0

您能否請給我舉個例子,謝謝您的幫助 –