Android Studio,通过json文件解析时不推荐使用的方法

Android Studio,通过json文件解析时不推荐使用的方法,android,json,parsing,deprecated,Android,Json,Parsing,Deprecated,我正在尝试创建一个简单的货币转换器移动应用程序。我已经学习了一些教程,例如:youtube上的NewBoston()和这个。我现在走到了死胡同,似乎找不到解决办法。我的猜测是不推荐使用的方法导致了程序崩溃,但我对它们是什么很不熟悉,或者我可能是错的。我正不顾一切地想弄清楚问题出在哪里,所以我能得到的任何帮助都将不胜感激。这是我的密码: public class MainActivity extends ActionBarActivity { TextView httpStuff; HttpCl

我正在尝试创建一个简单的货币转换器移动应用程序。我已经学习了一些教程,例如:youtube上的NewBoston()和这个。我现在走到了死胡同,似乎找不到解决办法。我的猜测是不推荐使用的方法导致了程序崩溃,但我对它们是什么很不熟悉,或者我可能是错的。我正不顾一切地想弄清楚问题出在哪里,所以我能得到的任何帮助都将不胜感激。这是我的密码:

public class MainActivity extends ActionBarActivity {

TextView httpStuff;
HttpClient client;
JSONObject json;
final static String URL = "http://openexchangerates.org/api/latest.json?app_id=1fdb20f2772748569ede3ff56127d8c3";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    httpStuff = (TextView) findViewById(R.id.tvhttp);
    client = new DefaultHttpClient();
    new Read().execute("EUR");
}
public JSONObject lastTweet() throws IOException, JSONException {
     StringBuilder url = new StringBuilder(URL);

     HttpGet get = new HttpGet(url.toString());
     HttpResponse r = client.execute(get);
     int status = r.getStatusLine().getStatusCode();
     if (status == 200) {
        HttpEntity e = r.getEntity();
        String data = EntityUtils.toString(e);
        JSONArray timeline = new JSONArray(data);
        JSONObject last = timeline.getJSONObject(0);
       return last;
}
else {
    return null;
}}


public class Read extends AsyncTask<String,Integer,String> {
    @Override
    protected String doInBackground(String... params) {
        try {
            json = lastTweet();
            return json.getString(params[0]);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
    protected void onPostExecute(String result){
        httpStuff.setText(result);
    }
}}
公共类MainActivity扩展了ActionBarActivity{
TextView-httpStuff;
HttpClient;
JSONObject json;
最终静态字符串URL=”http://openexchangerates.org/api/latest.json?app_id=1fdb20f2772748569ede3ff56127d8c3";
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
httpStuff=(TextView)findviewbyd(R.id.tvhttp);
client=新的DefaultHttpClient();
新读()执行(“欧元”);
}
公共JSONObject lastTweet()抛出IOException,JSONException{
StringBuilder url=新的StringBuilder(url);
HttpGet=newhttpget(url.toString());
HttpResponse r=client.execute(get);
int status=r.getStatusLine().getStatusCode();
如果(状态==200){
HttpEntity e=r.getEntity();
字符串数据=EntityUtils.toString(e);
JSONArray时间线=新JSONArray(数据);
JSONObject last=timeline.getJSONObject(0);
最后返回;
}
否则{
返回null;
}}
公共类读取扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…参数){
试一试{
json=lastweet();
返回json.getString(参数[0]);
}捕获(IOE异常){
e、 printStackTrace();
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
受保护的void onPostExecute(字符串结果){
httpStuff.setText(结果);
}
}}

根据您的代码(假设您将SDK版本定为API级别22),我的最佳猜测是您使用的是弃用的
HttpClient
API。根据佩奇的说法,它已被弃用。基本上,您不应该使用
HttpClient
,而应该使用
HttpURLConnection
,如本文所述

因此,不要在
lastweet()
方法中使用
HttpGet
HttpResponse
,而是使用以下方法:

 URL url = new URL(URL);
 HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
 try {
   InputStream in = new BufferedInputStream(urlConnection.getInputStream());
   readStream(in);
 finally {
   urlConnection.disconnect();
 }
}

来源于Android。

您可以添加您的导入语句和logcat响应吗