-2

这行代码给了我错误:

 HttpPost httppost = new HttpPost("file:///android_asset/www/me.json");

而不是文件,即使我使用"http://xyz......");我仍然得到错误./........
import android.os.Bundle; 导入android.app.Activity;导入 android.webkit.WebView;导入 android.widget.TextView;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    TextView TV=(TextView)findViewById(R.id.tv1);
    DefaultHttpClient   httpclient = new DefaultHttpClient(new BasicHttpParams());
    HttpPost httppost = new HttpPost("file:///android_asset/www/me.json");

    InputStream inputStream = null;

    try {
        HttpResponse response = httpclient.execute(httppost);           
        HttpEntity entity = response.getEntity();
        String result=null;
        inputStream = entity.getContent();
        // json is UTF-8 by default
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"), 8);
        StringBuilder sb = new StringBuilder();

        String line = null;
        while ((line = reader.readLine()) != null)
        {
            sb.append(line + "\n");
        }
        result = sb.toString();
        TV.setText(result);
    } catch (Exception e) { 
       Log.e("MYAPP", "exception", e);
    }
    finally {
        try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
    }
    }

}

这是json文件

{"resp": {"status": true, "version": "2.0", "artist": {"profile": "An Australian rock band, formed in 1973 by Angus and Malcolm Young, they teamed up with Dave Evans (vocals), Larry Van Kriedt (bass) and Colin Burgess (drums). In 1974 both Larry Van Kriedt and Colin Burgess left and were replaced by Rob Bailey (bass) and Peter Clack (drums), a further change in 1974 saw Peter Clack leave and Tony Currenti (drums) join the band. In June 1974 they were signed by Harry Vanda & George Young (Malcolm & Angus's brother) to Albert Productions. In November 1974, Dave Evans left the band and was replaced by Bon Scott (vocals & bagpipes). Rob Bailey also left in 1974 and was replaced by George Young (bass). In 1975 Phil Rudd (drums) replaced Tony Currenti and Mark Evans (bass) replaced George Young.  In June 1977 Mark Evans left and is replaced by Cliff Williams (bass) for their first tour of the USA. On the 19 Feb 1980 Bon Scott died at the age of 33. Brian Johnson (ex Geordie) joined the band to replace him on vocals and the album \"Back In Black\" was released, a tribute to Bon Scott, this album became the 2nd largest selling album of all time with over 40 million copies sold worldwide. In May 1983, Phil Rudd had a parting of the ways and was replaced by Simon Wright (drums), aged 20 then. November 1989 Simon Wright left and is replaced by Chris Slade (ex Manfred Mann's Earth Band, Uriah Heep & The Firm). In summer 1994 Phil Rudd \"quietly\" rejoined the band.\r\n\r\nAC/DC are Australia's most successful rock band ever, and are popular around the world.\r\nThe band was inducted into Rock And Roll Hall Of Fame in 2003 as a performer.\r\n\r\nCurrent line-up:\r\nAngus Young (lead guitar)\r\nMalcolm Young (rhythm guitar)\r\nBrian Johnson (vocals)\r\nCliff Williams (bass guitar)\r\nPhil Rudd (drums)\n", "releases_url": "http://api.discogs.com/artists/84752/releases", "name": "AC/DC", "uri": "http://www.discogs.com/artist/AC%2FDC", "members": ["Angus Young", "Bon Scott", "Brian Johnson", "Chris Slade", "Cliff Williams", "Colin Burgess", "Dave Evans", "Larry Van Kriedt", "Malcolm Young", "Mark Evans (3)", "Phil Rudd", "Simon Wright (4)"], "urls": ["http://www.acdcrocks.com/", "http://www.acdc.com/", "http://www.acdcpower.net/", "http://www.myspace.com/acdc", "http://en.wikipedia.org/wiki/AC/DC"], "images": [{"uri": "http://api.discogs.com/image/A-84752-1233004620.jpeg", "height": 309, "width": 418, "resource_url": "http://api.discogs.com/image/A-84752-1233004620.jpeg", "type": "primary", "uri150": "http://api.discogs.com/image/A-150-84752-1233004620.jpeg"}, {"uri": "http://api.discogs.com/image/A-84752-1094915280.jpg", "height": 313, "width": 300, "resource_url": "http://api.discogs.com/image/A-84752-1094915280.jpg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-84752-1094915280.jpg"}, {"uri": "http://api.discogs.com/image/A-84752-1105107816.jpg", "height": 129, "width": 180, "resource_url": "http://api.discogs.com/image/A-84752-1105107816.jpg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-84752-1105107816.jpg"}, {"uri": "http://api.discogs.com/image/A-84752-1107645658.jpg", "height": 199, "width": 200, "resource_url": "http://api.discogs.com/image/A-84752-1107645658.jpg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-84752-1107645658.jpg"}, {"uri": "http://api.discogs.com/image/A-84752-1182165014.jpeg", "height": 335, "width": 498, "resource_url": "http://api.discogs.com/image/A-84752-1182165014.jpeg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-84752-1182165014.jpeg"}, {"uri": "http://api.discogs.com/image/A-84752-1233004627.jpeg", "height": 360, "width": 480, "resource_url": "http://api.discogs.com/image/A-84752-1233004627.jpeg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-84752-1233004627.jpeg"}, {"uri": "http://api.discogs.com/image/A-84752-1233004633.jpeg", "height": 247, "width": 457, "resource_url": "http://api.discogs.com/image/A-84752-1233004633.jpeg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-84752-1233004633.jpeg"}, {"uri": "http://api.discogs.com/image/A-84752-1233004641.jpeg", "height": 376, "width": 400, "resource_url": "http://api.discogs.com/image/A-84752-1233004641.jpeg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-84752-1233004641.jpeg"}, {"uri": "http://api.discogs.com/image/A-84752-1233004648.jpeg", "height": 389, "width": 572, "resource_url": "http://api.discogs.com/image/A-84752-1233004648.jpeg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-84752-1233004648.jpeg"}, {"uri": "http://api.discogs.com/image/A-84752-1297548279.jpeg", "height": 414, "width": 600, "resource_url": "http://api.discogs.com/image/A-84752-1297548279.jpeg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-84752-1297548279.jpeg"}, {"uri": "http://api.discogs.com/image/A-84752-1294786426.jpeg", "height": 450, "width": 450, "resource_url": "http://api.discogs.com/image/A-84752-1294786426.jpeg", "type": "secondary", "uri150": "http://api.discogs.com/image/A-150-84752-1294786426.jpeg"}], "resource_url": "http://api.discogs.com/artists/84752", "id": 84752, "data_quality": "Correct", "namevariations": ["AC / DC", "AC DC", "AC-DC", "AC//DC", "ACDC", "DC/AC", "\u042d\u0439 \u0421\u0438 \u0414\u0438 \u0421\u0438"]}}}
4

2 回答 2

1

创建一个包含字符串的 jsonobject,然后解析该 jsonObject 中的每个项目,如下所示:

我的回应 =

{"resp": {"status": true, "version": ...

代码 :

JSONObject json_main = new JSONObject(MyResponse);
JSONObject c = json_main.getJSONObject("resp");
final String status= c.getString("status");

例子

如何使用:{“resp”:{“status”:true,“version”:“2.0”},“resp”:{“status”:true,“version”:“3.0”}

   JSONObject json_main = new JSONObject(MyResponse);   
    JSONArray main_arr = json_main.getJSONArray("resp");                                 for(int i = 0; i < main_arr.length(); i++)
    {
      JSONObject c = main_arr.getJSONObject(i);
      final String status = c.getString("status ");
    }

在此处输入图像描述

于 2013-08-13T20:45:27.353 回答
0

我认为您在这里遇到的问题是file://在您的 HTTP Post 中使用该协议。我不确定这是否有效(但我不是安卓人)。看起来您在“www”文件夹中有该文件,因此它必须是某种网络服务器。为什么不使用http://协议访问它。你也不需要在那里发帖。您可以使用 HTTP Get 就好了。

如果您确实想使用 file 访问它,请使用文件阅读器来读取文件,如下所示:

//Read text from file
StringBuilder text = new StringBuilder();

try {
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line;

    while ((line = br.readLine()) != null) {
        text.append(line);
        text.append('\n');
    }
}
catch (IOException e) {
    //You'll need to add proper error handling here
}
于 2013-08-13T21:23:09.463 回答