从mysql数据库接收数据并在TextView上显示

时间:2017-01-21 17:31:26

标签: android mysql

我试图从sql表接收数据并将其显示在TextView上。这是我到目前为止的代码..没有错误。什么都没发生。我使用AsyncTask从数据库中读取并使用OnpostExecute来写入数据

package com.example.mendex.medphil;
/**
* Created by Manuka on 1/21/2017.
*/

public class ProfileLoad extends AsyncTask<Void, Void, String>{
public static String array[];
String name;
Context context;
private Context mContext;
private View rootView;

// public void RecuperarComentarisFoto(Context context, View rootView){
  //  this.mContext=context;
    //this.rootView=rootView;
//}
ProfileLoad(Context ctx) {
    context = ctx;
}

@Override
protected String doInBackground(Void... params) {
    // These two need to be declared outside the try/catch
    // so that they can be closed in the finally block.
    HttpURLConnection urlConnection = null;
    BufferedReader reader = null;

    // Will contain the raw JSON response as a string.
    String forecastJsonStr = null;
    try {
        URL url = new URL("http://192.168.43.248/profileLoad.php");

        // Create the request to OpenWeatherMap, and open the connection
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.connect();

        // Read the input stream into a String
        InputStream inputStream = urlConnection.getInputStream();
        StringBuffer buffer = new StringBuffer();
        if (inputStream == null) {
            // Nothing to do.
            return null;
        }
        reader = new BufferedReader(new InputStreamReader(inputStream));

        String line;
        while ((line = reader.readLine()) != null) {

            buffer.append(line + "\n");
        }

        if (buffer.length() == 0) {
            // Stream was empty.  No point in parsing.
            return null;
        }
        forecastJsonStr = buffer.toString();
        int length = buffer.length();
        // public String[] array = new String[length];
        JSONObject o = new JSONObject(forecastJsonStr);
        JSONArray a = o.getJSONArray("result");
        array = new String[a.length()];
        for (int i = 0; i < a.length(); i++) {
            JSONObject object = a.getJSONObject(i);
            array[i] = object.getString("FullName");
            name = array[i];
           // System.out.print(name);
        }
        return name;
    } catch (Exception e) {
        Log.e("PlaceholderFragment", "Error ", e);
        return null;
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
        if (reader != null) {
            try {
                reader.close();
            } catch (final IOException e) {
                Log.e("PlaceholderFragment", "Error closing stream", e);
            }
        }
    }
}

@Override
protected void onPostExecute(String e) {
   // ListView Name = (ListView) rootView.findViewById(R.id.list);
    //final ArrayList<String> NameList =
    TextView name = (TextView)rootView.findViewById(R.id.Name);
    name.setText(e);
 }
 }

这不起作用。我做错了什么吗?

0 个答案:

没有答案