PHP MySQL

A

andele78

Neues Mitglied
0
Hi Zusammen, Versuche über PHP/MySQL eine ROW in eine TextView einzulesen ,komme zu keinen ergebniss.

Code:
class BackgroundTask extends AsyncTask<Void,Void,String>
{
    String json_url;
    @Override
    protected void onPreExecute() {
        json_url = VTCConfig.GETVTC1;
    }

    @Override
    protected String doInBackground(Void... voids) {
        try {
            URL url = new URL(json_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder stringBuilder = new StringBuilder();
            while ((JSON_STRING = bufferedReader.readLine())!=null)
            {
             stringBuilder.append(JSON_STRING+"\n");
            }

            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return stringBuilder.toString().trim();


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String result) {
        TextView textView = (TextView)findViewById(R.id.vtc30_1_txt);
        textView.setText(result);
    }
}
 
Hallo Andele,

es kommt darauf an, wie du dein Json anforderst - GET ??
Um dir helfen zu können , brauchen wir ein wenig mehr input , wie deine Serverseite aussieht

P.S ich vermisse auch deinen Json Parser .......

Tip : Es ist natürlich nicht verkehrt , alles erst mal "zu Fuss" zu machen (zum Lernen) , allerdings tust du Dir hiermit eher einen Gefallen :)
GitHub - koush/ion: Android Asynchronous Networking and Image Loading
Simple, stabil und schöne Callbacks
 
Zuletzt bearbeitet:
Im TextView wird das angezeigt : [{"name":"Robert"}]
möchte aber da nur Robert angezeigt wird.

Sorry ....hier ist der Parser und die PHP:
Code:
public class JSONParser {
   static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    public JSONObject getJSONFromUrl(String url) {

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        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();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
}



Code:
<?php
$mysql_db_hostname = "localhost";
$mysql_db_user = "********l";
$mysql_db_password = "***********";
$mysql_db_database = "**********";



$con = @mysqli_connect($mysql_db_hostname, $mysql_db_user, $mysql_db_password,
 $mysql_db_database);

if (!$con) {
 trigger_error('Could not connect to MySQL: ' . mysqli_connect_error());
}
$var = array();
 $sql = "SELECT name FROM ex1 ORDER BY id DESC LIMIT 2,1";
$result = mysqli_query($con, $sql);

while($obj = mysqli_fetch_object($result)) {
$var[] = $obj;
}
echo json_encode($var);
?>
 
Hallo,
du gibst das ganze JSON Object aus, du musst aber nur den namen aus dem Object holen :

Code:
String name = jObj.getString("name");

und dann in das TextView schreiben.

Gruß
 
  • Danke
Reaktionen: andele78 und swa00
Vielen Dank......funktioniert......Schones WE....!
 

Ähnliche Themen

B
Antworten
0
Aufrufe
689
basementmedia
B
KL7000F
  • KL7000F
Antworten
9
Aufrufe
1.005
KL7000F
KL7000F
H
Antworten
7
Aufrufe
1.641
haner
H
Zurück
Oben Unten