过去4天我一直想弄清楚这一点,我准备把头发拉出来!
我需要你的帮助,任何人都可以告诉我我做错了什么吗?
以下是我用于开发的JSON格式链接:已删除隐私权问题
这是我的代码:
public class JSONActivity extends Activity {
TextView http;
HttpClient client;
JSONObject json;
final static String URL = "REMOVED FOR PRIVACY CONCERNS
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
http = (TextView) findViewById(R.id.http);
client = new DefaultHttpClient();
new Read().execute("firstName");
}
public JSONObject getpw(String password)
throws ClientProtocolException, IOException, JSONException {
StringBuilder url = new StringBuilder(URL);
url.append(password);
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 getname = new JSONArray(data);
JSONObject last = getname.getJSONObject(3);
return last;
} else {
Toast.makeText(JSONActivity.this, "error", Toast.LENGTH_SHORT);
return null;
}
}
public class Read extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
try {
json=getpw("trustme");
return json.getString(arg0[0]);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
http.setText(result);
}
}}
非常感谢任何帮助。
答案 0 :(得分:2)
您期待JSONArray,但webservice只是返回一个JSONObject。
我做了以下更改:
JSONArray getname = new JSONArray(data);
- &gt; JSONObject getname = new JSONObject(data);
在异步任务中,我做了以下更改:
return json.getString("firstName");
这是完整的代码:
public class PlaygroundActivity extends Activity {
TextView http;
HttpClient client;
JSONObject json;
final static String URL = "REMOVED FOR PRIVACY CONCERNS
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
http = (TextView) findViewById(R.id.http);
client = new DefaultHttpClient();
new Read().execute("firstName");
}
public JSONObject getpw(String password) throws ClientProtocolException,
IOException, JSONException {
StringBuilder url = new StringBuilder(URL);
url.append(password);
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);
JSONObject getname = new JSONObject(data);
return getname;
} else {
Toast.makeText(PlaygroundActivity.this, "error", Toast.LENGTH_SHORT);
return null;
}
}
public class Read extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
try {
json = getpw("trustme");
return json.getString("firstName");
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
http.setText(result);
}
}
}
答案 1 :(得分:0)
您可以使用以下代码从服务器获取响应,然后解析json。
public static void extractParameters(){
String urlRequest = "REMOVED FOR PRIVACY CONCERNS";
String response = getServerResponse(urlRequest);
try {
JSONObject json = new JSONObject(response);
boolean suspended = json.getBoolean("suspended");
String firstName = json.getString("firstName");
String lastName = json.getString("lastName");
int checkdin = json.getInt("checkedin");
int checkindatetime = json.getInt("checkindatetime");
//Get Json object address
JSONObject address = json.getJSONObject("address");
String streedAddress = address.getString("streetAddress");//In same way get city etc
//Get PhoneNumber Array
JSONArray phoneNumbers = json.getJSONArray("phoneNumber");
String type = phoneNumbers.getJSONObject(0).getString("type");
String number = phoneNumbers.getJSONObject(0).getString("number");// and so on for 1,2,3...
} catch (Exception e) {
// TODO: handle exception
}
}
这些方法有助于从服务器获取响应
/**
*
* @param urlRequest URL of server
* @return
*/
public static String getServerResponse(String urlRequest){
Log.d("urlRequest",urlRequest);
String response = "";
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) new URL(urlRequest).openConnection();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
response = read(conn.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
Log.d("response",response);
return response.trim();
}
private static String read(InputStream in) throws IOException {
StringBuilder sb = new StringBuilder();
BufferedReader r = new BufferedReader(new InputStreamReader(in), 1000);
for (String line = r.readLine(); line != null; line = r.readLine()) {
sb.append(line);
}
in.close();
return sb.toString();
}
您发送的JSON响应示例为:
{
"suspended": "false",
"firstName": "John",
"lastName": "Smith",
"checkedin": 0,
"checkindatetime": 0,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
},
"phoneNumber": [
{
"type": "home",
"number": "212 111-1111"
},
{
"type": "fax",
"number": "646 222-2222"
}
]
}