Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid and PHP connection to MySQL Database
    primarykey
    data
    text
    <p>i have the following code and i am struggling to get it working . There are 2 errors in the java file and i can not find a fix.</p> <p>here is my MySQLData.java file</p> <pre><code>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 is = 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(); is = entity.getContent(); }catch(Exception e){ Log.e("log_tag", "Error in http connection"+e.toString()); } //convert response to 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&lt;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(); } } } </code></pre> <p>There is an error on the following 2 lines: </p> <pre><code>String result=null; httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); </code></pre> <p>The first error says duplicate local variable result, and the second error says nameValuePairs cannot be resolved to a variable.</p> <p>i also have a php file that i put in my wamp server and i would like the infro thats showing up on my local server to be in the application. I read somewhere that i might have to port forward so that i could acces from the app over the internet, but i don't know. The app wont run until the errors are fixed.</p> <p>Here is my PHP file fetch_data2.php</p> <pre><code> &lt;?php mysql_connect("888888","8888888","*******"); mysql_select_db("*******"); $q=mysql_query("SELECT * FROM users"); while($row=mysql_fetch_assoc($q)) $output[]=$row; print(json_encode($output)); mysql_close(); ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload