P
ackage com;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import bb.mobrep.R;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class JSONActivity extends Activity {
public String readJSONFeed(String URL) {
StringBuilder stringBuilder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(URL);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
Log.w("str", line.toString());
stringBuilder.append(line);
}
} else {
Log.e("JSON", "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return stringBuilder.toString();
}
private class ReadJSONFeedTask extends AsyncTask<String, Void, String> {
protected String doInBackground(String... urls) {
return readJSONFeed(urls[0]);
}
protected void onPostExecute(String result) {
try {
JSONArray jsonArray = new JSONArray(result);
Log.i("JSON", "Number of surveys in feed: " + jsonArray.length());
Toast.makeText(getBaseContext(), jsonArray.toString(), Toast.LENGTH_SHORT).show();
// ---print out the content of the json feed---
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
// Toast.makeText(getBaseContext(),
// jsonObject.getString("text") +
// " - " + jsonObject.getString("created_at"),
// Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.jjson);
new ReadJSONFeedTask().execute("http://220.247.247.130/b.txt");
}
}
无法获取完整字符串,结束于“C00000047”,“”nam。有人可以帮我吗
但是这个http://220.247.247.130/c.txt
文件工作正常。
如何下载冗长的JSON文件并显示
JSON:
{
"customers": [
{
"id": "C0000002",
"nam": "Asian Pharmacy",
"add": "125 Main Street Kattankudy "
},
{
"id": "C0000003",
"nam": "Asiri Pharmaceuticals",
"add": "213 Galahitiyawa Ganemulla aa"
},
{
"id": "C0000004",
"nam": "Badulla Apothecaries",
"add": "229 Lower Street Badulla. "
},
{
"id": "C0000005",
"nam": "B-Line Pharma",
"add": "Carmel Estate Ibbagamuwa "
},
{
"id": "C0000006",
"nam": "A.K.I. ABEYRATHNA",
"add": "47/K/18 SRI WICKRAMA MW SAMMANTHRANA PURA COLOMBO-15"
},
{
"id": "C0000007",
"nam": "Chamee Chemist",
"add": "41275 Magalegoda Veyangoda. "
},
{
"id": "C0000008",
"nam": "Chamee Pharmaceuticals (Pvt) Ltd",
"add": "561/28 New Bus Stand Anuradhapura "
},
{
"id": "C0000009",
"nam": "Chandrasiri Chemists",
"add": "279B Morris Road Maitipe Galle."
},
{
"id": "C0000010",
"nam": "CHL Pharmaceuticals",
"add": "41 E Railway Avenue Nugegoda. "
},
{
"id": "C0000011",
"nam": "City Medicals",
"add": "487 Hospital Road Jaffna "
},
{
"id": "C0000012",
"nam": "City Medicals",
"add": "433 Dockyard Road Trincomalee. "
},
{
"id": "C0000013",
"nam": "Crystal Drugs",
"add": "3 Donald Janz Road Bataganwila Galle."
},
{
"id": "C0000014",
"nam": "Crystal Pharmaceuticals",
"add": "362/1 Dangedara Galle. "
},
{
"id": "C0000015",
"nam": "Damith Distributors",
"add": "275/5 1st Lane Mandawila Road Kesbewa Piliyandala."
},
{
"id": "C0000016",
"nam": "Galle Drug Stores",
"add": "14 Ambagamuwa Road Gampola "
},
{
"id": "C0000017",
"nam": "Gamage Distributors",
"add": "217/C Jaya Mawatha Kadawatha. "
},
{
"id": "C0000018",
"nam": "H.G. Pharmaceuticals",
"add": "24 1st Cross Street Talbet Town Galle."
},
{
"id": "C0000019",
"nam": "Ishini Pharmaceuticals",
"add": "104/6A Templers Road Mt. Lavinia. "
},
{
"id": "C0000020",
"nam": "Kandy Medicals",
"add": "41282 Market Road Mawanella. "
},
{
"id": "C0000021",
"nam": "Kurunegala Medicals",
"add": "4 Weavers Shopping Complex Mihindu Mawatha Kurunegala."
},
{
"id": "C0000022",
"nam": "Lloyed Pharmacy",
"add": "1A Anagarika Dharmapala Mawatha Medagoda Matara."
},
{
"id": "C0000023",
"nam": "M.D.J. Distributors",
"add": "21 St. Joseph's Road Nugegoda. "
},
{
"id": "C0000024",
"nam": "Medicare Enterprises",
"add": "394/6 Biyagama Road Gonawala Kelaniya."
},
{
"id": "C0000025",
"nam": "Mowlana Pharmacy",
"add": "52 Main Street Kalmunai. "
},
{
"id": "C0000026",
"nam": "Nanda Pharmacy",
"add": "2 Fort Road Chilaw. "
},
{
"id": "C0000027",
"nam": "Neelan Pharmaceuticals (Pvt) Ltd",
"add": "Jaffna Road Kilinochchi"
},
{
"id": "C0000028",
"nam": "New Eleans Marketing",
"add": "110 Town Mosque Road Akkaraipattu. "
},
{
"id": "C0000029",
"nam": "New Lanka Pharmacy",
"add": "280 Galle Road Aluthgama. "
},
{
"id": "C0000030",
"nam": "Noor Pharmaceuticals Pvt Ltd",
"add": "50 Main Street Kalmunai. "
},
{
"id": "C0000031",
"nam": "Pharmaco (Pvt) Ltd",
"add": "77 Main Street Negombo "
},
{
"id": "C0000032",
"nam": "Raaj Medicals Stores",
"add": "91 Kandy Road Vavuniya. "
},
{
"id": "C0000033",
"nam": "Ragama Pharmacy",
"add": "34 Tewatte Road Ragama. "
},
{
"id": "C0000034",
"nam": "Ransika Distributors",
"add": "151/11 Tewatta Road Ragama "
},
{
"id": "C0000035",
"nam": "SAS Pharmaceuticals",
"add": "Pahala Ranawana Dewalegama. "
},
{
"id": "C0000036",
"nam": "Sathsara Enterprises",
"add": "495 Mihindu Mawatha Pitipana North Homagama."
},
{
"id": "C0000037",
"nam": "Sigma Impex",
"add": "933 Maradana Road Colombo 08. "
},
{
"id": "C0000038",
"nam": "Southern Pharmaceuticals (Pvt) Ltd",
"add": "1605/32 Crystal Terrace Malabe Road Kottawa Pannipitiya"
},
{
"id": "C0000039",
"nam": "Sri Lanka Pharmacy (Pvt) Ltd",
"add": "39 D S Senanayake Veediya Kandy. "
},
{
"id": "C0000040",
"nam": "Supreme Pharmaceutical Care Distributors",
"add": "30/81 Ramya Gardens Kalapaluwawa Rajagiriya."
},
{
"id": "C0000041",
"nam": "T.C. Distributors",
"add": "315 Galle Road Mt. Lavinia. "
},
{
"id": "C0000042",
"nam": "The New Pharmacy",
"add": "85A Kandy Road Kurunegala. "
},
{
"id": "C0000043",
"nam": "Thusitha Distributors & Pharmacy",
"add": "64 Central Street Hingurakgoda "
},
{
"id": "C0000044",
"nam": "United Pharma (Pvt) Ltd",
"add": "18 Havelock Place Galle. "
},
{
"id": "C0000045",
"nam": "United Pharmacy",
"add": "429/A Pita Kotte Kotte. "
},
{
"id": "C0000046",
"nam": "Viththy Med Engineering",
"add": "22 A St. Anthanys Road Batticaloa. "
},
{
"id": "C0000047",
"nam": "Wettasinghe Pharmaceuticals",
"add": "86 Uyana Road Moratuwa. "
},
{
"id": "C0000048",
"nam": "Wickramasooriya Enterprises",
"add": "Carmel Estate Bakmeegolla Ibbagamuwa "
},
{
"id": "C0000049",
"nam": "Wijemanne Grocers",
"add": "43 Main Street Horana. "
},
{
"id": "C0000078",
"nam": "1a",
"add": "2b 3c 4d 5e"
},
{
"id": "C0000079",
"nam": "dd",
"add": "dd dd dd "
},
{
"id": "C0000080",
"nam": "saliya",
"add": "add1 add 2 add 3 add 4 "
},
{
"id": "C0000081",
"nam": "wasim mirza",
"add": "a11 a22 a33 a44"
},
{
"id": "C0000082",
"nam": "tharindu",
"add": " "
}
]
}
答案 0 :(得分:0)
将字符串转换为JsonObject
JSONObject jObj = null;
jObj = new JSONObject(json);
System.out.print(jObj.toString());
然后,您可以在JSONObject上使用.get方法来检索像
这样的对象值jObj.get('nameofyourvariable');
P.S。 json应该是string,一个json字符串。
答案 1 :(得分:0)
您的字符串是JSONOBject。
protected void onPostExecute(String result) {
try {
JSONObject jsonObject = new JSONObject(result);
// Log.i("JSON", "Number of surveys in feed: " + jsonArray.length());
JSONArray ja = jsonObject.getJSONArray("customers");
// Toast.makeText(MainActivity.this, jsonArray.toString(), Toast.LENGTH_SHORT).show();
// ---print out the content of the json feed---
for (int i = 0; i < ja.length(); i++) {
JSONObject jb = ja.getJSONObject(i);
String s = jb.getString("id");
Log.i("ID is ",s);
String nam = jb.getString("nam");
Log.i("Nam is",nam);
String add = jb.getString("add");
Log.i("Add is",add);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
您的doInbackground
。将String _response声明为类变量。
protected String doInBackground(String... urls) {
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(urls[0]);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
_response=EntityUtils.toString(entity);
}
}
catch (Exception e) {
e.printStackTrace();
}
return _response;
}
编辑:
{
表示json对象节点
[
表示json数组节点。
{ // your string is a json object
"customers": [ // json array of customers
{ // json object
"id": "C0000002" // json id is string
我刚试了一下。这就是我得到的。
07-13 08:29:26.239: I/ID is(1571): C0000002
07-13 08:29:26.239: I/Nam is(1571): Asian Pharmacy
07-13 08:29:26.239: I/Add is(1571): 125 Main Street Kattankudy
07-13 08:29:26.239: I/ID is(1571): C0000003
07-13 08:29:26.239: I/Nam is(1571): Asiri Pharmaceuticals
07-13 08:29:26.249: I/Add is(1571): 213 Galahitiyawa Ganemulla aa
07-13 08:29:26.249: I/ID is(1571): C0000004
07-13 08:29:26.259: I/Nam is(1571): Badulla Apothecaries
07-13 08:29:26.259: I/Add is(1571): 229 Lower Street Badulla.
07-13 08:29:26.259: I/ID is(1571): C0000005
07-13 08:29:26.259: I/Nam is(1571): B-Line Pharma
07-13 08:29:26.259: I/Add is(1571): Carmel Estate Ibbagamuwa
07-13 08:29:26.259: I/ID is(1571): C0000006
07-13 08:29:26.259: I/Nam is(1571): A.K.I. ABEYRATHNA
07-13 08:29:26.259: I/Add is(1571): 47/K/18 SRI WICKRAMA MW SAMMANTHRANA PURA COLOMBO-15
07-13 08:29:26.259: I/ID is(1571): C0000007
07-13 08:29:26.269: I/Nam is(1571): Chamee Chemist
07-13 08:29:26.269: I/Add is(1571): 41275 Magalegoda Veyangoda.
07-13 08:29:26.269: I/ID is(1571): C0000008
07-13 08:29:26.269: I/Nam is(1571): Chamee Pharmaceuticals (Pvt) Ltd
07-13 08:29:26.269: I/Add is(1571): 561/28 New Bus Stand Anuradhapura
07-13 08:29:26.269: I/ID is(1571): C0000009
07-13 08:29:26.269: I/Nam is(1571): Chandrasiri Chemists
07-13 08:29:26.269: I/Add is(1571): 279B Morris Road Maitipe Galle.
07-13 08:29:26.269: I/ID is(1571): C0000010
07-13 08:29:26.269: I/Nam is(1571): CHL Pharmaceuticals
07-13 08:29:26.279: I/Add is(1571): 41 E Railway Avenue Nugegoda.
07-13 08:29:26.309: I/ID is(1571): C0000011
07-13 08:29:26.309: I/Nam is(1571): City Medicals
07-13 08:29:26.309: I/Add is(1571): 487 Hospital Road Jaffna
07-13 08:29:26.309: I/ID is(1571): C0000012
07-13 08:29:26.309: I/Nam is(1571): City Medicals
07-13 08:29:26.309: I/Add is(1571): 433 Dockyard Road Trincomalee.
07-13 08:29:26.320: I/ID is(1571): C0000013
07-13 08:29:26.320: I/Nam is(1571): Crystal Drugs
07-13 08:29:26.320: I/Add is(1571): 3 Donald Janz Road Bataganwila Galle.
07-13 08:29:26.320: I/ID is(1571): C0000014
07-13 08:29:26.320: I/Nam is(1571): Crystal Pharmaceuticals
07-13 08:29:26.320: I/Add is(1571): 362/1 Dangedara Galle.
07-13 08:29:26.329: I/ID is(1571): C0000015
07-13 08:29:26.329: I/Nam is(1571): Damith Distributors
07-13 08:29:26.329: I/Add is(1571): 275/5 1st Lane Mandawila Road Kesbewa Piliyandala.
07-13 08:29:26.339: I/ID is(1571): C0000016
07-13 08:29:26.339: I/Nam is(1571): Galle Drug Stores
07-13 08:29:26.339: I/Add is(1571): 14 Ambagamuwa Road Gampola
07-13 08:29:26.339: I/ID is(1571): C0000017
07-13 08:29:26.339: I/Nam is(1571): Gamage Distributors
07-13 08:29:26.339: I/Add is(1571): 217/C Jaya Mawatha Kadawatha.
07-13 08:29:26.339: I/ID is(1571): C0000018
07-13 08:29:26.349: I/Nam is(1571): H.G. Pharmaceuticals
07-13 08:29:26.349: I/Add is(1571): 24 1st Cross Street Talbet Town Galle.
07-13 08:29:26.349: I/ID is(1571): C0000019
07-13 08:29:26.349: I/Nam is(1571): Ishini Pharmaceuticals
07-13 08:29:26.349: I/Add is(1571): 104/6A Templers Road Mt. Lavinia.
07-13 08:29:26.349: I/ID is(1571): C0000020
07-13 08:29:26.359: I/Nam is(1571): Kandy Medicals
07-13 08:29:26.359: I/Add is(1571): 41282 Market Road Mawanella.
07-13 08:29:26.359: I/ID is(1571): C0000021
07-13 08:29:26.369: D/dalvikvm(1571): GC_CONCURRENT freed 149K, 10% free 2663K/2932K, paused 6ms+53ms, total 190ms
07-13 08:29:26.379: I/Nam is(1571): Kurunegala Medicals
07-13 08:29:26.379: I/Add is(1571): 4 Weavers Shopping Complex Mihindu Mawatha Kurunegala.
07-13 08:29:26.379: I/ID is(1571): C0000022
07-13 08:29:26.379: I/Nam is(1571): Lloyed Pharmacy
07-13 08:29:26.391: I/Add is(1571): 1A Anagarika Dharmapala Mawatha Medagoda Matara.
07-13 08:29:26.391: I/ID is(1571): C0000023
07-13 08:29:26.391: I/Nam is(1571): M.D.J. Distributors
07-13 08:29:26.391: I/Add is(1571): 21 St. Josephs Road Nugegoda.
07-13 08:29:26.391: I/ID is(1571): C0000024
07-13 08:29:26.391: I/Nam is(1571): Medicare Enterprises
07-13 08:29:26.391: I/Add is(1571): 394/6 Biyagama Road Gonawala Kelaniya.
07-13 08:29:26.399: I/ID is(1571): C0000025
07-13 08:29:26.399: I/Nam is(1571): Mowlana Pharmacy
07-13 08:29:26.399: I/Add is(1571): 52 Main Street Kalmunai.
07-13 08:29:26.399: I/ID is(1571): C0000026
07-13 08:29:26.399: I/Nam is(1571): Nanda Pharmacy
07-13 08:29:26.409: I/Add is(1571): 2 Fort Road Chilaw.
07-13 08:29:26.409: I/ID is(1571): C0000027
07-13 08:29:26.409: I/Nam is(1571): Neelan Pharmaceuticals (Pvt) Ltd
07-13 08:29:26.419: I/Add is(1571): Jaffna Road Kilinochchi
07-13 08:29:26.419: I/ID is(1571): C0000028
07-13 08:29:26.429: I/Nam is(1571): New Eleans Marketing
07-13 08:29:26.429: I/Add is(1571): 110 Town Mosque Road Akkaraipattu.
07-13 08:29:26.429: I/ID is(1571): C0000029
07-13 08:29:26.429: I/Nam is(1571): New Lanka Pharmacy
07-13 08:29:26.429: I/Add is(1571): 280 Galle Road Aluthgama.
07-13 08:29:26.429: I/ID is(1571): C0000030
07-13 08:29:26.429: I/Nam is(1571): Noor Pharmaceuticals Pvt Ltd
07-13 08:29:26.429: I/Add is(1571): 50 Main Street Kalmunai.
07-13 08:29:26.439: I/ID is(1571): C0000031
07-13 08:29:26.439: I/Nam is(1571): Pharmaco (Pvt) Ltd
07-13 08:29:26.439: I/Add is(1571): 77 Main Street Negombo
07-13 08:29:26.439: I/ID is(1571): C0000032
07-13 08:29:26.439: I/Nam is(1571): Raaj Medicals Stores
07-13 08:29:26.439: I/Add is(1571): 91 Kandy Road Vavuniya.
07-13 08:29:26.439: I/ID is(1571): C0000033
07-13 08:29:26.449: I/Nam is(1571): Ragama Pharmacy
07-13 08:29:26.449: I/Add is(1571): 34 Tewatte Road Ragama.
07-13 08:29:26.449: I/ID is(1571): C0000034
07-13 08:29:26.449: I/Nam is(1571): Ransika Distributors
07-13 08:29:26.449: I/Add is(1571): 151/11 Tewatta Road Ragama
07-13 08:29:26.449: I/ID is(1571): C0000035
07-13 08:29:26.449: I/Nam is(1571): SAS Pharmaceuticals
07-13 08:29:26.449: I/Add is(1571): Pahala Ranawana Dewalegama.
07-13 08:29:26.449: I/ID is(1571): C0000036
07-13 08:29:26.459: I/Nam is(1571): Sathsara Enterprises
07-13 08:29:26.459: I/Add is(1571): 495 Mihindu Mawatha Pitipana North Homagama.
07-13 08:29:26.459: I/ID is(1571): C0000037
07-13 08:29:26.459: I/Nam is(1571): Sigma Impex
07-13 08:29:26.459: I/Add is(1571): 933 Maradana Road Colombo 08.
07-13 08:29:26.459: I/ID is(1571): C0000038
07-13 08:29:26.469: I/Nam is(1571): Southern Pharmaceuticals (Pvt) Ltd
07-13 08:29:26.469: I/Add is(1571): 1605/32 Crystal Terrace Malabe Road Kottawa Pannipitiya
07-13 08:29:26.469: I/ID is(1571): C0000039
07-13 08:29:26.469: I/Nam is(1571): Sri Lanka Pharmacy (Pvt) Ltd
07-13 08:29:26.469: I/Add is(1571): 39 D S Senanayake Veediya Kandy.
07-13 08:29:26.469: I/ID is(1571): C0000040
07-13 08:29:26.481: I/Nam is(1571): Supreme Pharmaceutical Care Distributors
07-13 08:29:26.481: I/Add is(1571): 30/81 Ramya Gardens Kalapaluwawa Rajagiriya.
07-13 08:29:26.481: I/ID is(1571): C0000041
07-13 08:29:26.489: I/Nam is(1571): T.C. Distributors
07-13 08:29:26.489: I/Add is(1571): 315 Galle Road Mt. Lavinia.
07-13 08:29:26.489: I/ID is(1571): C0000042
07-13 08:29:26.489: I/Nam is(1571): The New Pharmacy
07-13 08:29:26.489: I/Add is(1571): 85A Kandy Road Kurunegala.
07-13 08:29:26.489: I/ID is(1571): C0000043
07-13 08:29:26.489: I/Nam is(1571): Thusitha Distributors & Pharmacy
07-13 08:29:26.499: I/Add is(1571): 64 Central Street Hingurakgoda
07-13 08:29:26.499: I/ID is(1571): C0000044
07-13 08:29:26.499: I/Nam is(1571): United Pharma (Pvt) Ltd
07-13 08:29:26.499: I/Add is(1571): 18 Havelock Place Galle.
07-13 08:29:26.499: I/ID is(1571): C0000045
07-13 08:29:26.499: I/Nam is(1571): United Pharmacy
07-13 08:29:26.499: I/Add is(1571): 429/A Pita Kotte Kotte.
07-13 08:29:26.499: I/ID is(1571): C0000046
07-13 08:29:26.499: I/Nam is(1571): Viththy Med Engineering
07-13 08:29:26.509: I/Add is(1571): 22 A St. Anthanys Road Batticaloa.
07-13 08:29:26.509: I/ID is(1571): C0000047
07-13 08:29:26.509: I/Nam is(1571): Wettasinghe Pharmaceuticals
07-13 08:29:26.509: I/Add is(1571): 86 Uyana Road Moratuwa.
07-13 08:29:26.519: I/ID is(1571): C0000048
07-13 08:29:26.519: I/Nam is(1571): Wickramasooriya Enterprises
07-13 08:29:26.519: I/Add is(1571): Carmel Estate Bakmeegolla Ibbagamuwa
07-13 08:29:26.519: I/ID is(1571): C0000049
07-13 08:29:26.519: I/Nam is(1571): Wijemanne Grocers
07-13 08:29:26.519: I/Add is(1571): 43 Main Street Horana.
07-13 08:29:26.519: I/ID is(1571): C0000078
07-13 08:29:26.519: I/Nam is(1571): 1a
07-13 08:29:26.519: I/Add is(1571): 2b 3c 4d 5e
07-13 08:29:26.519: I/ID is(1571): C0000079
07-13 08:29:26.519: I/Nam is(1571): dd
07-13 08:29:26.519: I/Add is(1571): dd dd dd
07-13 08:29:26.529: I/ID is(1571): C0000080
07-13 08:29:26.529: I/Nam is(1571): saliya
07-13 08:29:26.529: I/Add is(1571): add1 add 2 add 3 add 4
07-13 08:29:26.529: I/ID is(1571): C0000081
07-13 08:29:26.529: I/Nam is(1571): wasim mirza
07-13 08:29:26.529: I/Add is(1571): a11 a22 a33 a44
07-13 08:29:26.539: I/ID is(1571): C0000082
07-13 08:29:26.539: I/Nam is(1571): tharindu
07-13 08:29:26.539: I/Add is(1571):