2016-11-28 5 views
-4

アンドロイドでユーザー入力パラメータを使用してJSONからデータを取得するにはどうすればよいですか?私は一部を呼び出し、あなたのJavaファイルにユーザー入力パラメータでアンドロイドでJSON GETメソッドを解析する方法は?

private static JSONObject get(Context ctx, String sUrl) { 
    HttpURLConnection connection = null; 

    try { 

     URL url = new URL(sUrl); 
     connection = (HttpURLConnection) url.openConnection(); 
     connection.setRequestProperty("Content-Type", "application/json"); 
     connection.setRequestProperty("Accept", "application/json"); 
     connection.setRequestProperty("Authorization", 
       "Basic " + encodedAuthentication); 
     connection.setRequestProperty("Accept-Charset", "utf-8,*"); 
     Log.d("Get-Request", url.toString()); 
     try { 
      BufferedReader bufferedReader = new BufferedReader(
        new InputStreamReader(connection.getInputStream())); 
      StringBuilder stringBuilder = new StringBuilder(); 
      String line; 
      while ((line = bufferedReader.readLine()) != null) { 
       stringBuilder.append(line).append("\n"); 
      } 
      bufferedReader.close(); 
      Log.d("Get-Response", stringBuilder.toString()); 
      return new JSONObject(stringBuilder.toString()); 
     } finally { 
      connection.disconnect(); 
     } 
    } catch (Exception e) { 
     Log.e("ERROR", e.getMessage(), e); 
     return null; 
    } 
} 

private static String buildSanitizedRequest(String url, 
              Map<String, String> mapOfStrings) { 

    Uri.Builder uriBuilder = new Uri.Builder(); 
    uriBuilder.encodedPath(url); 
    if (mapOfStrings != null) { 
     for (Map.Entry<String, String> entry : mapOfStrings.entrySet()) { 
      Log.d("buildSanitizedRequest", "key: " + entry.getKey() 
        + " value: " + entry.getValue()); 
      uriBuilder.appendQueryParameter(entry.getKey(), 
        entry.getValue()); 
     } 
    } 
    String uriString; 
    try { 
     uriString = uriBuilder.build().toString(); // May throw an 
     // UnsupportedOperationException 
    } catch (Exception e) { 
     Log.e("Exception", "Exception" + e); 
    } 

    return uriBuilder.build().toString(); 

} 

そして、あなたのJSONを以下のコードを貼り付け

、以下のコードは、あなたの問題の実行可能解を与えるだろうと考えてい

+0

こんにちはSurya、http://stackoverflow.com/help/how-to-askを見て、あなたはその問題の完全な説明を投稿するべきです。 – Bethan

+0

@ Bクマール、GETメソッドを使用してJSONを解析する方法は? – suryac

+0

JSONを投稿することもできますか –

答えて

2

この

のようになります。
public static JSONObject exampleGetMethod(Context ctx, String sUrl,String yourName) throws JSONException, IOException { 
    Map<String, String> request = new HashMap<String, String>(); 
    request.put("yourName", yourName); 

    sUrl = sUrl + "yourApiName"; 
    return get(ctx, buildSanitizedRequest(sUrl, request)); 
} 

上記のコードでは、startDate、endDate、yourNameおよびusernameが入力パラメータです。 sUrlはAPI url + API名です 最後にexampleGetMethod(Context,String,String,String,String,String); を呼び出すと、リクエストされたURLのJSON応答が返されます。

あなたが

ロジック以下
JSONArray a = response.getJSONArray("contacts"); 
JSONObject needyArray; 
for (int i = 0; i < a.length(); i++) { 
     if(a.getJSONObject(i).optString("name").equals("kumar")){ 
      needyArray = a.getJSONObject(i); 
      break; 
     } 
} 

を考える必要がある応答から特定の配列値を取得したい場合は今needyArray JSONObject変数は(例としてクマー)特定の人のデータを持っている

+0

を使用して特定の連絡先の名前と詳細を取得する方法例:このJSONでapi.androidhive.info/contacts名前を渡すことによって特定の連絡先の名前とその詳細を取得する方法。 – suryac

+0

ちょっとSurya、私は少し私の答えを編集し、以下のリンクはあなたに多くの助けになるかもしれない、ちょうどこれを試してみてくださいhttp://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/ – Bethan

+0

ありがとうございます。しかし、すべてのjsonデータを表示します。 jsonオブジェクトに入力を渡して特定の詳細を取得する方法は?たとえば、 "クマール"の詳細を表示する場合。 jsonオブジェクトに "kumar"を渡す方法は? – suryac

関連する問題