Twitchストリームに関するデータを格納するために、HTTPサーバーから文字列(実際にはJSONファイル)を取得しようとしています。
接続して取得した文字列:api.twitch.tv/kraken/streams/lainkk
この文字列を文字列変数で取得し、この文字列からJSONオブジェクトを作成したいと思います。
私はいくつかのメソッドを作成しましたが、それらのメソッドを呼び出している間、常にログにSystemErrが記録されました。
これは私自身のTwitchAPIクラスです:
package com.linkpulsion.bibix;
import android.util.Log;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class Twitch {
private String channel;
public final static String TWITCH_API_SERVER = "https://api.twitch.tv/kraken/";
/**
* Constructor of Java Twitch API Json Generator
* @param channel The cannel name to get the infos
*/
public Twitch(String channel){
if(channel != null){
this.channel = channel;
}
}
/**
* Getter for channel name
* @return String the channel name
*/
public String getChannel(){
return this.channel;
}
protected String getJson(String mode){
String apiUri = TWITCH_API_SERVER + mode + "/" + this.channel;
try{
URL website = new URL(apiUri);
URLConnection connection = website.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null)
response.append(inputLine);
in.close();
return response.toString();
}catch (Exception e){
e.printStackTrace();
return null;
}
}
protected JSONObject buildJSON(String jsonRaw){
JSONObject json = null;
try{
json = new JSONObject(jsonRaw);
}catch (Exception e){
e.printStackTrace();
}
return json;
}
public void isSreaming(){
String jsonRaw = getJson("streams");
Log.d(null,"RETOUR " + jsonRaw);
}
}
ありがとうございました !
元のコードを実行したところ、NetworkOnMainThreadExceptionが発生しました。
簡単なAsyncTaskを作成しましたが、機能しました。
private class TwitchAsync extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
String str = twitch.getJson("streams");
return str;
}
@Override
protected void onPostExecute(String result){
Toast.makeText(MainActivity.this, "result: " + result, Toast.LENGTH_LONG).show();
//testing buildJSON:
JSONObject obj = twitch.buildJSON(result);
Log.d("JSON Result: ", obj.toString());
}
}
これが私がテストに使用した完全なクラスです:
public class MainActivity extends AppCompatActivity {
Twitch twitch;
JSONObject jsonObj; //instance variable which can be accessed anywhere in the class code
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
twitch = new Twitch("aksynial");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
System.out.println("REFRESHING");
new TwitchAsync().execute();
return true;
}
return super.onOptionsItemSelected(item);
}
private class TwitchAsync extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
String str = twitch.getJson("streams");
return str;
}
@Override
protected void onPostExecute(String result){
Toast.makeText(MainActivity.this, "result: " + result, Toast.LENGTH_LONG).show();
//testing buildJSON:
//assign to the instance variable jsonObj
jsonObj = twitch.buildJSON(result);
Log.d("JSON Result: ", jsonObj.toString());
}
}
}
結果を記録するonPostExecute()
:
D/JSON Result:﹕ {"_links":{"channel":"https:\/\/api.twitch.tv\/kraken\/channels\/aksynial","self":"https:\/\/api.twitch.tv\/kraken\/streams\/aksynial"},"stream":null}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加