从PHP(在线数据库)到Android的Android连接不起作用


Android connection from PHP (online database) to Android is not working

我正在尝试在我的网站上检索一个特定的字符串。http://plpemag.byethost31.com/functions.php?action=printSpecificArticle&articleid=2通过android,并将其显示在我的文本视图中。但是当我运行程序时,文本视图仍然是空白的。这可能有什么问题?

GetMethodEx.java

package com.example.navigationdrawerexample;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class GetMethodEx {
    public String getInternetData() throws Exception{
        BufferedReader in = null;
        String data = null;
        try{
            HttpClient client = new DefaultHttpClient();
            URI website = new URI("http://plpemag.byethost31.com/functions.php?action=printSpecificArticle&articleid=2");
            HttpGet request = new HttpGet();
            request.setURI(website);
            HttpResponse response = client.execute(request);
            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String l = "";
            String nl = System.getProperty("line.separator");
            while ((l = in.readLine()) !=null){
                sb.append(l+nl);
            }
            in.close();
            data = sb.toString();
            return data;
        }finally{
            if (in != null){
                try{
                    in.close();
                    return data;
                } catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
}

CollegeBulletinListFragment.java

package com.example.navigationdrawerexample;

import android.app.Fragment;
import android.os.Bundle;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class CollegeBulletinListFragment extends Fragment{
    public CollegeBulletinListFragment(){
    }
    TextView kem;
    TextView tester;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_collegebulletinlist, container, false);
        kem = (TextView) rootView.findViewById(R.id.angkembular);
        tester = (TextView) rootView.findViewById(R.id.tester);
        Bundle bun = getArguments();
        setTextAndColorsToHead(bun.getString("passingWord"));
        GetMethodEx gm = new GetMethodEx();
        String returned;
        try {
            returned = gm.getInternetData();
            tester.setText(returned);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return rootView;
    }
    public void setTextAndColorsToHead(String arg){
        if (arg.equals("GN")){
            kem.setText("General News");
        } else if (arg.equals("CCS")){
            kem.setText("College of Computer Studies");
        } else if (arg.equals("COE")){
            kem.setText("College of Engineering");
        } else if (arg.equals("COED")){
            kem.setText("College of Education");
        } else if (arg.equals("CON")){
            kem.setText("College of Nursing");
        } else if (arg.equals("CBA")){
            kem.setText("College of Business and Accountancy");
        } else if (arg.equals("CAS")){
            kem.setText("College of Arts and Sciences");
        } else if (arg.equals("CIHM")){
            kem.setText("College of International and Hospitality Management");
        } 
    }
}

链接:http://plpemag.byethost31.com/functions.php?action=printSpecificArticle&articleid=2给出了从数据库中提取的数据的json格式。现在我们需要使用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);
        }
    }

您还需要Apiconnector类

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;
    }

希望这能有所帮助。