以下JSON將被接收,但通過http://localhost/getData.php
而是拋出異常從JSON獲取數據和org.json.JSONException拋出
{"username":"not found","password":null}
登錄我收到
02-19 17:31:54.745: E/JSON Parser(5277): Error parsing data org.json.JSONException: End of input at character 0 of
02-19 17:31:59.185: E/JSON Parse(5277): ERROR
下面的代碼是方法在那裏拋出異常
@Override
protected String doInBackground(String... url){
try{
String resultText = "";
EditText edit = (EditText)findViewById(R.id.text_box);
id = edit.getText().toString();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id",id));
JSONObject json = jsonParser.HttpRequest("http://localhost/getData.php", params);
resultText = json.getString("username");
}catch(Exception e){
e.printStackTrace();
Log.e("JSON Parse","ERROR");
}
return "";
}
public class SimpleJsonParser {
public SimpleJsonParser(){
}
public JSONObject HttpRequest(String url, List<NameValuePair> params){
InputStream input = null;
JSONObject jObj = null;
String json = "";
String line;
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
paramsString = URLEncodedUtils.format(params,"utf-8");
url += "?" + paramsString;
HttpGet httpGet = new HttpGet(url);
try{
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
HttpEntity entity = response.getEntity();
input = entity.getContent();
BufferedReader reader =
new BufferedReader(new InputStreamReader(input));
while((line = reader.readLine())!=null){
builder.append(line);
}
json = builder.toString();
} else {
Log.e("JSON Parser","Failed to download file");
}
}catch(ClientProtocolException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
try {
jObj = new JSONObject(json);
input.close();
} catch (Exception e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}
}
什麼錯我的代碼?我提供的代碼是發生異常的地方
是的,訪問本地主機,這是Android設備,而不是你的服務器。 – njzk2 2013-02-19 17:45:11