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();
}
}
}`
ありがとうございます!それはうまくいった!私は本当に迷惑になりました。私は必要なオブジェクトを取得していませんでした...それは、Jsonがサーバーによって返されたことは、それが閲覧されたブラウザとOSに依存することが判明しました。 –
あなたは大歓迎です!あなたは私の答えを受け入れられた答えとupvoteとしてマークできますか? :) –
ありがとう、私は遅刻して申し訳ありません。私は上向きにした –