我下载了尝试测试它的示例代码但我无法正确连接到我的托管数据库。当我尝试加载它时,我的应用程序崩溃了。我查看了我的logcat,但无法理解它。
我的Logcat:
07-30 22:39:00.702: D/AndroidRuntime(23520): Shutting down VM
07-30 22:39:00.702: W/dalvikvm(23520): threadid=1: thread exiting with uncaught exception (group=0x4194ada0)
07-30 22:39:00.702: E/AndroidRuntime(23520): FATAL EXCEPTION: main
07-30 22:39:00.702: E/AndroidRuntime(23520): Process: selva.select, PID: 23520
07-30 22:39:00.702: E/AndroidRuntime(23520): java.lang.NullPointerException
07-30 22:39:00.702: E/AndroidRuntime(23520): at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
07-30 22:39:00.702: E/AndroidRuntime(23520): at org.json.JSONTokener.nextValue(JSONTokener.java:94)
07-30 22:39:00.702: E/AndroidRuntime(23520): at org.json.JSONArray.<init>(JSONArray.java:92)
07-30 22:39:00.702: E/AndroidRuntime(23520): at org.json.JSONArray.<init>(JSONArray.java:108)
07-30 22:39:00.702: E/AndroidRuntime(23520): at selva.select.DbselectActivity$1.onClick(DbselectActivity.java:88)
07-30 22:39:00.702: E/AndroidRuntime(23520): at android.view.View.performClick(View.java:4633)
07-30 22:39:00.702: E/AndroidRuntime(23520): at android.view.View$PerformClick.run(View.java:19330)
07-30 22:39:00.702: E/AndroidRuntime(23520): at android.os.Handler.handleCallback(Handler.java:733)
07-30 22:39:00.702: E/AndroidRuntime(23520): at android.os.Handler.dispatchMessage(Handler.java:95)
07-30 22:39:00.702: E/AndroidRuntime(23520): at android.os.Looper.loop(Looper.java:157)
07-30 22:39:00.702: E/AndroidRuntime(23520): at android.app.ActivityThread.main(ActivityThread.java:5356)
07-30 22:39:00.702: E/AndroidRuntime(23520): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 22:39:00.702: E/AndroidRuntime(23520): at java.lang.reflect.Method.invoke(Method.java:515)
07-30 22:39:00.702: E/AndroidRuntime(23520): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
07-30 22:39:00.702: E/AndroidRuntime(23520): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
07-30 22:39:00.702: E/AndroidRuntime(23520): at dalvik.system.NativeStart.main(Native Method)
07-30 22:39:03.194: I/Process(23520): Sending signal. PID: 23520 SIG: 9
MainActivity:
package selva.select;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
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.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TableRow.LayoutParams;
public class DbselectActivity extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
String result = null;
InputStream is = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("drinkdealsapplicationdublin.net78.net/selectall.php");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("log_tag", "connection success ");
// Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection "+e.toString());
Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show();
}
//convert response to string
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");
// Toast.makeText(getApplicationContext(), "Input Reading pass", Toast.LENGTH_SHORT).show();
}
is.close();
result=sb.toString();
}
catch(Exception e)
{
Log.e("log_tag", "Error converting result "+e.toString());
Toast.makeText(getApplicationContext(), " Input reading fail", Toast.LENGTH_SHORT).show();
}
//parse json data
try
{
JSONArray jArray = new JSONArray(result);
String re=jArray.getString(jArray.length()-1);
TableLayout tv=(TableLayout) findViewById(R.id.table);
tv.removeAllViewsInLayout();
int flag=1;
for(int i=-1;i<jArray.length()-1;i++)
{
TableRow tr=new TableRow(DbselectActivity.this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
if(flag==1)
{
TextView b6=new TextView(DbselectActivity.this);
b6.setText("ID");
b6.setTextColor(Color.BLUE);
b6.setTextSize(15);
tr.addView(b6);
TextView b19=new TextView(DbselectActivity.this);
b19.setPadding(10, 0, 0, 0);
b19.setTextSize(15);
b19.setText("Name");
b19.setTextColor(Color.BLUE);
tr.addView(b19);
TextView b29=new TextView(DbselectActivity.this);
b29.setPadding(10, 0, 0, 0);
b29.setText("no");
b29.setTextColor(Color.BLUE);
b29.setTextSize(15);
tr.addView(b29);
tv.addView(tr);
final View vline = new View(DbselectActivity.this);
vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2));
vline.setBackgroundColor(Color.BLUE);
tv.addView(vline);
flag=0;
}
else
{
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","id: "+json_data.getInt("f1")+
", Username: "+json_data.getString("f2")+
", No: "+json_data.getInt("f3"));
TextView b=new TextView(DbselectActivity.this);
String stime=String.valueOf(json_data.getInt("f1"));
b.setText(stime);
b.setTextColor(Color.RED);
b.setTextSize(15);
tr.addView(b);
TextView b1=new TextView(DbselectActivity.this);
b1.setPadding(10, 0, 0, 0);
b1.setTextSize(15);
String stime1=json_data.getString("f2");
b1.setText(stime1);
b1.setTextColor(Color.WHITE);
tr.addView(b1);
TextView b2=new TextView(DbselectActivity.this);
b2.setPadding(10, 0, 0, 0);
String stime2=String.valueOf(json_data.getInt("f3"));
b2.setText(stime2);
b2.setTextColor(Color.RED);
b2.setTextSize(15);
tr.addView(b2);
tv.addView(tr);
final View vline1 = new View(DbselectActivity.this);
vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
vline1.setBackgroundColor(Color.WHITE);
tv.addView(vline1);
}
}
}
catch(JSONException e)
{
Log.e("log_tag", "Error parsing data "+e.toString());
Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
}
}
});
}
}
PHP脚本:
<?php
$con = mysql_connect('XXXXXXXXX', 'XXXXXXXX', 'XXXXXXX');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("a9588589_drink", $con);
$i=mysql_query("select * from t1",$con);
$num_rows = mysql_num_rows($i);
$check='';
while($row = mysql_fetch_array($i))
{
$r[]=$row;
$check=$row['f1'];
}
if($check==NULL)
{
$r[$num_rows]="Record is not available";
print(json_encode($r));
}
else
{
$r[$num_rows]="success";
print(json_encode($r));
}
mysql_close($con);
?>
链接到PHP脚本:http://drinkdealsapplicationdublin.net78.net/selectall.php
我不认为我需要包含任何其他内容,但我从http://androidprogramz.blogspot.ie/2012/07/retrieve-database-from-mysql-and.html获得了教程
如果我遗漏了任何东西并感谢您的时间,请道歉。
另外,如果您需要XML文件:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_below="@+id/e1"
android:layout_width="276dp"
android:layout_height="wrap_content"
android:text="Button" />
<ScrollView
android:layout_below="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusableInTouchMode="true"
android:focusable="true">
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusableInTouchMode="true"
android:focusable="true">
<TableLayout
android:id="@+id/table"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusableInTouchMode="true"
android:focusable="true">
</TableLayout>
</HorizontalScrollView>
</ScrollView>
</RelativeLayout>
编辑:更多信息:当我单击按钮加载数据时,它会崩溃。该应用程序启动