我正在尝试提取Json数据,但不知何故代码无效。 有人能帮帮我吗 ?似乎无法理解我做错了什么。
(我真的没有更多细节可以添加)
public class MainActivity extends AppCompatActivity{
TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t=(TextView)findViewById(R.id.exchangeRate);
Anindya task = new Anindya();
task.execute("xyz");
}
public class Anindya extends AsyncTask<String,Void,String>
{
@Override
protected String doInBackground(String... urls) {
String result = "";
URL url;
HttpURLConnection urlConnection=null;
try {
url = new URL(urls[0]);
urlConnection = (HttpURLConnection)url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(System.in);
int data = reader.read();
while (data!= 0)
{
char current = (char)data;
result += current;
data = reader.read();
}
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.i("website content",result);
}
}
}
答案 0 :(得分:1)
尝试将InputStreamReader reader = new InputStreamReader(System.in)
替换为InputStreamReader reader = new InputStreamReader(in)
如果仍然无法解决您的问题,请尝试检查data
的值并发布有关返回值的详细信息。