我尝试使用带有HTTP连接的Android应用程序连接到mysql数据库,然后重新启动JSONArray以扫描查询结果。这是代码:
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
new JSONfunction().execute();
}
private class JSONfunction extends AsyncTask<Void,Void,JSONArray> {
private JSONArray getJSONfromURL(String url) {
InputStream is = null;
String result = "";
JSONArray jArray = null;
// Download JSON data from URL
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// Convert response to string
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 + "\n");
}
is.close();
result = sb.toString();
jArray=new JSONArray(result);
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
return jArray;
}
@Override
protected JSONArray doInBackground(Void... voids) {
JSONArray jArray=getJSONfromURL("http://192.168.1.5/ConnectData/index.php");
return jArray;
}
protected void onPostExecute(JSONArray result) {
TextView prova = (TextView)findViewById(R.id.txtProva);
try {
JSONObject rec = result.getJSONObject(1);
prova.setText(rec.getString("first_name"));
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data " + e.toString());
}
}
}
我在执行while时遇到错误。在应用程序崩溃之后的2或3个周期之后。你能给我任何帮助吗?
[编辑]
我收到此错误: 11-28 20:42:19.006 27688-27702 / com.example.marco.provadatabase E / log_tag:转换结果时出错org.json.JSONException:值!java.lang.String类型的DOCTYPE无法转换为JSONArray
[编辑2]
好的我明白了这个问题。现在,我正面对一个新的。今天再次尝试执行代码,但现在我在线路上得到一个超时异常HttpResponse response = httpclient.execute(httppost); (昨天工作正常,我没有改变任何事情)。它不是一个URL问题,因为如果我将它放在浏览器上它可以正常工作。
答案 0 :(得分:0)
您的响应类型是String,您应该将其转换为JSONObject
答案 1 :(得分:0)
这是问题
result = sb.toString();
jArray=new JSONArray(result);
你正在尝试获取你的响应字符串的jsonarray,我认为它的根目录没有jsonarray。您能否根据您的要求分享回复。
您的超时问题
在我的示例中,设置了两个超时。连接超时抛出“java.net.SocketTimeoutException:Socket未连接”和套接字超时“java.net.SocketTimeoutException:操作超时”。
HttpGet httpGet = new HttpGet(url);
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
// The default value is zero, that means the timeout is not used.
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpResponse response = httpClient.execute(httpGet);
如果要设置任何现有HTTPClient的参数(例如DefaultHttpClient或AndroidHttpClient),可以使用函数setParams()。
httpClient.setParams(httpParameters);
为你清楚地说明事情:)