Android and PHP connection to MySQL Database -
i have following code , struggling working . there 2 errors in java file , can not find fix.
here mysqldata.java file
package com.example.qosmetre2; import java.io.bufferedreader; import java.io.inputstream; import java.io.inputstreamreader; import java.util.arraylist; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.namevaluepair; import org.apache.http.client.httpclient; import org.apache.http.client.entity.urlencodedformentity; import org.apache.http.client.methods.httppost; import org.apache.http.impl.client.defaulthttpclient; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import android.app.activity; import android.net.parseexception; import android.os.bundle; import android.util.log; import android.widget.toast; import java.util.arraylist; import java.util.hashmap; import java.util.list; import org.apache.http.namevaluepair; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import android.app.listactivity; import android.app.progressdialog; import android.content.intent; import android.os.asynctask; import android.os.bundle; import android.util.log; import android.view.view; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.listadapter; import android.widget.listview; import android.widget.simpleadapter; import android.widget.textview; public class mysqldata extends listactivity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); string result = null; inputstream = null; stringbuilder sb=null; string result=null; //http post try{ httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://localhost/android_test/fetch_data2.php"); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpresponse response = httpclient.execute(httppost); httpentity entity = response.getentity(); = entity.getcontent(); }catch(exception e){ log.e("log_tag", "error in http connection"+e.tostring()); } //convert response string try{ bufferedreader reader = new bufferedreader(new inputstreamreader(is,"iso-8859-1"),8); sb = new stringbuilder(); sb.append(reader.readline() + "\n"); string line="0"; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); result=sb.tostring(); }catch(exception e){ log.e("log_tag", "error converting result "+e.tostring()); } //paring data string nam; int rec_pow; int tow; int stat; try{ jsonarray jarray = new jsonarray(result); jsonobject json_data=null; for(int i=0;i<jarray.length();i++){ json_data = jarray.getjsonobject(i); nam=json_data.getstring("name"); rec_pow=json_data.getint("recieved_power"); tow=json_data.getint("tower"); stat=json_data.getint("status"); } }catch(jsonexception e1){ toast.maketext(getbasecontext(), "could not parse data", toast.length_long).show(); }catch (parseexception e1){ e1.printstacktrace(); } } }
there error on following 2 lines:
string result=null; httppost.setentity(new urlencodedformentity(namevaluepairs));
the first error says duplicate local variable result, , second error says namevaluepairs cannot resolved variable.
i have php file put in wamp server , infro thats showing on local server in application. read somewhere might have port forward acces app on internet, don't know. app wont run until errors fixed.
here php file fetch_data2.php
<?php mysql_connect("888888","8888888","*******"); mysql_select_db("*******"); $q=mysql_query("select * users"); while($row=mysql_fetch_assoc($q)) $output[]=$row; print(json_encode($output)); mysql_close(); ?>
in reviewing code, see variable result defined twice , don't see namevaluepairs defined anywhere. think compiler correct :})
Comments
Post a Comment