2016-12-21 8 views
0

http requestを解析しようとしたときに問題が発生しました...サンプルjsonを結果に挿入して、関連するすべてのコードをコメントアウトしようとしましたhttp requestが...私はそれがないHTTPリクエストを行うときにコードがfine.Butを働く見つけworking.Thank advance.Theは以下で、あなたは私のコード `httpリクエストを使用してJsonオブジェクトの解析エラーが発生しました

public class MainActivity extends AppCompatActivity { 

    String result = ""; 


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




    public void onclick(){ 
     Asynctaskrunner run = new Asynctaskrunner(); 
     run.execute("http://headers.jsontest.com/"); 
     TextView text = (TextView) findViewById(R.id.textView); 
     TextView text1 = (TextView) findViewById(R.id.textView2); 
     final TextView text2= (TextView) findViewById(R.id.textView5); 
     final TextView text3 = (TextView) findViewById(R.id.textView6); 
     Button button = (Button) findViewById(R.id.button); 

     button.setOnClickListener(
       new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         try { 

          JSONObject json = new JSONObject(result); 

          String name = json.getString("Accept-Language"); 
          String salary = json.getString("Host"); 
          text2.setText(name); 
          text3.setText(salary); 
         }catch (Exception e){ 
          e.printStackTrace(); 
         } 
        } 
       } 
     ); 



    } 
    //----------------------------------- 
    private class Asynctaskrunner extends AsyncTask<String,Void,Void>{ 

     private String response; 

     @Override 
     protected Void doInBackground(String... params) { 
      try { 
       URL url = new URL(params[0]); 
       HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
       conn.setRequestMethod("GET"); 
       InputStream in = new BufferedInputStream(conn.getInputStream()); 
       response=convertStreamToString(in); 

      } catch (MalformedURLException e) { 
       Log.e(TAG, "MalformedURLException: " + e.getMessage()); 
      } catch (ProtocolException e) { 
       Log.e(TAG, "ProtocolException: " + e.getMessage()); 
      } catch (IOException e) { 
       Log.e(TAG, "IOException: " + e.getMessage()); 
      } catch (Exception e) { 
       Log.e(TAG, "Exception: " + e.getMessage()); 
      } 


      result=response; 
      TextView textView = (TextView) findViewById(R.id.textView4); 

      return null; 
     } 


     private String convertStreamToString(InputStream is) { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
      StringBuilder sb = new StringBuilder(); 

      String line; 
      try { 
       while ((line = reader.readLine()) != null) { 
        sb.append(line).append('\n'); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } finally { 
       try { 
        is.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
      return sb.toString(); 
     } 


    } 

}` 

答えて

0

文字列名= json.getString(あります"Accept-Language");

問題はこちらです! JSONレスポンスはAccept-Languageキーの値を返しません。

これは応答です。

{ 
     "X-Cloud-Trace-Context": "8bc66db5c468f43daff9c6a69fa7d0be/11367517399452323805", 
     "Host": "headers.jsontest.com", 
     "User-Agent": "Dalvik/2.1.0 (Linux; U; Android 6.0.1; D6503 Build/23.5.A.0.570)" 
    } 

他のキーで変更してください!

String trace = json.getString("X-Cloud-Trace-Context"); 
String host = json.getString("Host"); 
String agent = json.getString("User-Agent"); 
+0

ありがとうございます!それはうまくいった!私は本当に迷惑になりました。私は必要なオブジェクトを取得していませんでした...それは、Jsonがサーバーによって返されたことは、それが閲覧されたブラウザとOSに依存することが判明しました。 –

+0

あなたは大歓迎です!あなたは私の答えを受け入れられた答えとupvoteとしてマークできますか? :) –

+1

ありがとう、私は遅刻して申し訳ありません。私は上向きにした –

関連する問題