doujiao1905 2015-11-12 16:26 采纳率: 100%
浏览 47

从PHP(在线数据库)到Android的Android连接无法正常工作

I'm trying to retrieve a specific string on my site. http://plpemag.byethost31.com/functions.php?action=printSpecificArticle&articleid=2 through android and display it on my textview. But when I run the program, the textView is still blank. What might be the problem for this?

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");
        } 
    }
}
  • 写回答

2条回答 默认 最新

  • duanhuren5581 2015-11-12 17:17
    关注

    The link: http://plpemag.byethost31.com/functions.php?action=printSpecificArticle&articleid=2 gives a json format of the data fetched from the database. Now we need to parse this json format using the variable name from the json format. For instance if we need content variable we can get it using

    @Override
            protected void onPostExecute(JSONArray jsonArray){
    
                setTextView(jsonArray);
    
            }
    

    The onPostExecute method is a part of the Asynctask. If you want that please let me know.

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

    Now you use the cont variable and assign it to the text view. Hope this helps.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog