Java 从PHP(在线数据库)到Android的Android连接不起作用
我正在尝试检索网站上的特定字符串。http://plpemag.byethost31.com/functions.php?action=printSpecificArticle&articleid=2 通过android并在我的文本视图上显示。但是当我运行程序时,textView仍然是空的。这可能有什么问题 GetMethodEx.java CollegeBulletinListFragment.java link:提供从数据库中获取的数据的json格式。现在我们需要使用json格式中的变量名来解析这个json格式。例如,如果我们需要内容变量,我们可以使用Java 从PHP(在线数据库)到Android的Android连接不起作用,java,php,android,android-fragments,httprequest,Java,Php,Android,Android Fragments,Httprequest,我正在尝试检索网站上的特定字符串。http://plpemag.byethost31.com/functions.php?action=printSpecificArticle&articleid=2 通过android并在我的文本视图上显示。但是当我运行程序时,textView仍然是空的。这可能有什么问题 GetMethodEx.java CollegeBulletinListFragment.java link:提供从数据库中获取的数据的json格式。现在我们需要使用json格式中的变量名来
@Override
protected void onPostExecute(JSONArray jsonArray){
setTextView(jsonArray);
}
onPostExecute方法是Asynctask的一部分。如果你想知道,请告诉我
setTextView(JSONArray jsonArray){
for(int i = 0; i<jsonArray.length();i++){
////////////////here you are iterating through all the arrays in json file.
try{
json = jsonArray.getJSONObject(i);
cont = json.getString("content");/////this brings the text in content variable to variable cont.
}catch(JSONException e){
e.printStackTrace();
}
}
现在使用cont变量并将其指定给文本视图。希望这有帮助。我也遇到了同样的问题,因为我没有使用异步任务
new GetTextTask().execute(new ApiConnector());
/////////////////call this when you want to parse the json page.
下面是异步任务
private class GetTextTask extends AsyncTask<ApiConnector,Long,JSONArray>{
@Override
protected JSONArray doInBackground(ApiConnector... params) {
return params[0].GetText();
}
@Override
protected void onPostExecute(JSONArray jsonArray){
setTextView(jsonArray);
}
}
希望这能有所帮助。您能帮我一点什么是Asynctask以及它的实现吗:但情况是,上面的代码不会像URL上的输出那样返回任何数据或字符串。如果您觉得有帮助,请您标记一下答案。
new GetTextTask().execute(new ApiConnector());
/////////////////call this when you want to parse the json page.
private class GetTextTask extends AsyncTask<ApiConnector,Long,JSONArray>{
@Override
protected JSONArray doInBackground(ApiConnector... params) {
return params[0].GetText();
}
@Override
protected void onPostExecute(JSONArray jsonArray){
setTextView(jsonArray);
}
}
public class ApiConnector {
public JSONArray GetText(){
String url = "your url for php";
HttpEntity httpEntity = null;
try{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
httpEntity = httpResponse.getEntity();
}catch (ClientProtocolException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
JSONArray jsonArray = null;
if(httpEntity != null){
try{
String entityResponse = EntityUtils.toString(httpEntity);
Log.e("Entity Response: ", entityResponse);
jsonArray = new JSONArray(entityResponse);
}catch(JSONException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
return jsonArray;
}