Results 1 to 1 of 1
Thread: Connecting Java to my database
- 02-02-2012, 06:18 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 1
- Rep Power
- 0
Connecting Java to my database
Hello I am pretty new at java and creating an android app. For some reason I can not get my webview to display the data in my database, it "force closes". Also can someone just confirm I am doing my intent correctly, I would like the main list view open in webview then once an item is clicked it will go to another listview. here is the code below with the php code
And here is my PHPJava Code:package the.review; 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.ListActivity; import android.content.Intent; import android.net.ParseException; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class ClassReviewActivity extends ListActivity { int un_id; String[] un_name = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.main); String result = null; InputStream is = null; StringBuilder sb=null; //http post try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://10.0.2.2/UniversityPage.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 JSONArray jArray; try{ jArray = new JSONArray(result); JSONObject json_data=null; un_name=new String[jArray.length()]; for(int i=0;i<jArray.length();i++){ json_data = jArray.getJSONObject(i); un_id=json_data.getInt("ID"); un_name[i]=json_data.getString("University"); } } catch(JSONException e1){ Toast.makeText(getBaseContext(), "No University Found" ,Toast.LENGTH_LONG).show(); } catch (ParseException e1) { e1.printStackTrace(); } setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, un_name)); ListView lv; lv = getListView(); lv.setTextFilterEnabled(true); lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), un_name[position]+" was Clicked", Toast.LENGTH_SHORT).show(); if(position == 0) { Intent myIntent = new Intent (getApplicationContext(), (BenedictineCourses.class)); startActivityForResult(myIntent, 0); } } }); }
Thank you for any helpPHP Code:<?php mysql_connect("127.0.0.1" "root"); mysql_select_db("Professors"); $q=mysql_query("SELECT * FROM professors WHERE University like 'A%'"); while($row = mysql_fetch_assoc($sql)) $output[]=$row; print (json_encode($output)); mysql_close(); ?>
Similar Threads
-
connecting Ms-access database with java applet
By balaKannan in forum New To JavaReplies: 1Last Post: 12-23-2010, 12:16 PM -
Connecting with database in java
By UJJAL DHAR in forum New To JavaReplies: 3Last Post: 06-05-2010, 07:34 AM -
Java connecting to sql database
By markious in forum New To JavaReplies: 3Last Post: 04-14-2010, 09:58 PM -
Connecting to Teradata Database from java
By drkpradeep in forum Advanced JavaReplies: 5Last Post: 02-18-2010, 09:16 AM -
Problem connecting to my Database using Java
By javaneed in forum New To JavaReplies: 3Last Post: 08-13-2007, 09:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks