私は過去数日間、Web APIから成功したJSON呼び出しを取得しようとしています。私は成功していない複数のAPIを試したので、API自体ではないと思いますが、どうすればHttpURLConnectionで呼び出すことができますか?HttpURLConnectionを使用してJSONレスポンス用のWeb APIでGETリクエストを使用するにはどうすればよいですか?
私が続き、これは無駄で動作するように取得しようと、複数のチュートリアルとガイドを使用しています。ここ
は、私のコードの剪定バージョンです。私はまた、BufferedReaderとStringBuilderを使用して、データを無駄にすることを試みました。EDIT:
public class NetworkConnect {
/**
* Execute the given URI, and return the data from that URI.
*
* @param uri the universal resource indicator for a set of data.
* @return the set of data provided by the uri
*/
private Exception exception;
HttpURLConnection connection = null;
BufferedReader reader = null;
protected String doInBackground(String urlString) {
try {
URL url = new URL(urlString);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line+"\n");
Log.d("Response: ", "> " + line); //here u ll get whole response...... :-)
}
return buffer.toString();
} catch (Exception e) {
e.printStackTrace();
}finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
あなたがいずれかを呼び出す必要がありますメインスレッドではないネットワーク要求 –
[どうすればandroid.os.NetworkOnMainThreadExceptionを修正できますか?](https://stackoverflow.com/questions/6343166/how-do-i-fix-android-os-networkonmainthreadexception) –
私はこの解決策を試したそれはうまくいかなかった。 – fangzthewolf