使用json从php:mysql到android接收数据
我正在开发一个Android应用程序,它从我的数据库发送和接收简单的fname,Lname。我正在使用php和mysql进行Web服务。我在用这个 在JSONParser类中,如下所示使用json从php:mysql到android接收数据,android,json,web-services,Android,Json,Web Services,我正在开发一个Android应用程序,它从我的数据库发送和接收简单的fname,Lname。我正在使用php和mysql进行Web服务。我在用这个 在JSONParser类中,如下所示 public class JSONParser { static InputStream is = null; static JSONObject jObj = null; static String json = ""; // constructor public JSONParser() { } //
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET method
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "talha");
}
Log.i("Buffer Error", "Baby Baby! Yes Mama! ");
is.close();
json = sb.toString();
Log.i("Buffer Error", "Eating suger! NO Mama! "+ json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("MyJSON", "Error parsing data "+ e.toString());
}
// return JSON String
return jObj;
}
}
我遇到异常JSONArray无法转换为JSONObject。请提供帮助,因为在其他每一个教程中,都会描述类似的过程。是的,如果您直接创建该过程,您将遇到该问题。请在
json=b.toString()之后添加一行代码>
那么,这里的错误是什么呢?当它试图在jObj=newjsonobject(json)处解析到json对象时出现异常;放入错误日志。您在menifest文件中有internet权限吗?该错误仅仅意味着您正在获取JSONArray作为回复,但您正在尝试将其作为JSONObject使用。使用新的JSONArray(json)您可以在以下位置向我们显示输出:Log.i(“缓冲区错误”,“吃糖!没有妈妈!”+json);现在我得到了这个异常->解析数据org.json.JSONException时出错:在{json_parse[[[{“name”:“fahad”},{“name”:“talha”},{“name”:“umer”},{“name”:“ahmed”},{“name”:“},{“name”:“ahmed”},{“abc”},{“name”:“ahmed perv”}]talha]}
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("MyJSON", "Error parsing data "+ e.toString());
}
json = sb.toString();
json = "{json_parse" + "[" + json + "]" + "}";