我尝试从HTTP服务器获取一个String(实际上是一个JSON文件)来存储有关Twitch流的数据。
我通过连接获得的字符串:api.twitch.tv/kraken/streams/lainkk
我想在String变量中获取此String并从此String构建一个JSON对象。
我写了一些方法但是在调用这些方法时我总是在日志中有一个SystemErr:
这是我自己的Twitch API类:
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);
}
}
谢谢!
答案 0 :(得分:0)
我运行了原始代码并获得了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}