我必须为我的应用程序从远程 mysql 数据库加载数据。该表包含事件 ID、事件名称和事件详细信息。我必须从表中获取最后一个数据并在 3 个文本视图中显示它。我尝试了来自 androidhive 的教程。但这似乎不起作用。请帮忙。
检索数据的类
class GetEventDetails extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading data from servers. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Getting product details in background thread
* */
protected String doInBackground(String... params) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
// Check for success tag
int success;
try {
// Building Parameters
//String pid="1";
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting product details by making HTTP request
// Note that product details url will use GET request
JSONObject json = jsonParser.makeHttpRequest(
url_product_detials, "GET", params);
// check your log for json response
Log.d("Single Product Details", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received product details
JSONArray eventObj = json
.getJSONArray(TAG_EVENT); // JSON Array
// get first product object from JSON Array
JSONObject event = eventObj.getJSONObject(0);
// product with this pid found
TextView txtEventname=(TextView) findViewById(R.id.textView2);
TextView txtEventdet=(TextView) findViewById(R.id.textView3);
// Edit Text
txtEventname.setText(event.getString(TAG_EVENTITLE));
txtEventdet.setText(event.getString(TAG_EVENTDET));
}else{
// product with pid not found
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return null;
}
}
Jason 解析器类
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET method
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
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;
}
}
我的错误是
04-28 15:43:29.918: D/dalvikvm(8036): Late-enabling CheckJNI
04-28 15:43:30.278: D/dalvikvm(8036): GC_FOR_ALLOC freed 78K, 12% free 9560K/10759K, paused 33ms, total 33ms
04-28 15:43:30.288: I/dalvikvm-heap(8036): Grow heap (frag case) to 10.705MB for 437672-byte allocation
04-28 15:43:30.358: D/dalvikvm(8036): GC_CONCURRENT freed <1K, 11% free 9986K/11207K, paused 13ms+7ms, total 70ms
04-28 15:43:30.358: D/dalvikvm(8036): WAIT_FOR_CONCURRENT_GC blocked 55ms
04-28 15:43:30.449: D/dalvikvm(8036): GC_FOR_ALLOC freed 2K, 11% free 10211K/11463K, paused 56ms, total 57ms
04-28 15:43:30.649: D/libEGL(8036): loaded /system/lib/egl/libEGL_mali.so
04-28 15:43:30.759: D/libEGL(8036): loaded /system/lib/egl/libGLESv1_CM_mali.so
04-28 15:43:30.789: D/libEGL(8036): loaded /system/lib/egl/libGLESv2_mali.so
04-28 15:43:30.879: D/OpenGLRenderer(8036): Enabling debug mode 0
04-28 15:43:32.130: D/dalvikvm(8036): GC_CONCURRENT freed 11K, 9% free 10906K/11975K, paused 4ms+6ms, total 56ms
04-28 15:43:32.130: D/dalvikvm(8036): WAIT_FOR_CONCURRENT_GC blocked 31ms
04-28 15:43:32.200: D/dalvikvm(8036): GC_FOR_ALLOC freed <1K, 10% free 11335K/12487K, paused 47ms, total 47ms
04-28 15:43:32.290: D/dalvikvm(8036): GC_CONCURRENT freed 3K, 8% free 11992K/12999K, paused 3ms+13ms, total 71ms
04-28 15:43:32.681: E/SpannableStringBuilder(8036): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-28 15:43:32.681: E/SpannableStringBuilder(8036): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-28 15:43:34.422: E/SpannableStringBuilder(8036): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-28 15:43:34.422: E/SpannableStringBuilder(8036): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
04-28 15:43:35.994: D/ProgressBar(8036): setProgress = 0
04-28 15:43:35.994: D/ProgressBar(8036): setProgress = 0, fromUser = false
04-28 15:43:35.994: D/ProgressBar(8036): mProgress = 0mIndeterminate = false, mMin = 0, mMax = 10000
04-28 15:43:36.124: W/ResourceType(8036): Failure getting entry for 0x010802c8 (t=7 e=712) in package 0 (error -75)
04-28 15:43:36.474: D/AndroidRuntime(8036): Shutting down VM
04-28 15:43:36.474: W/dalvikvm(8036): threadid=1: thread exiting with uncaught exception (group=0x41a162a0)
04-28 15:43:36.534: E/AndroidRuntime(8036): FATAL EXCEPTION: main
04-28 15:43:36.534: E/AndroidRuntime(8036): android.os.NetworkOnMainThreadException
04-28 15:43:36.534: E/AndroidRuntime(8036): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
04-28 15:43:36.534: E/AndroidRuntime(8036): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
04-28 15:43:36.534: E/AndroidRuntime(8036): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
04-28 15:43:36.534: E/AndroidRuntime(8036): at java.net.InetAddress.getAllByName(InetAddress.java:214)
04-28 15:43:36.534: E/AndroidRuntime(8036): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
04-28 15:43:36.534: E/AndroidRuntime(8036): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-28 15:43:36.534: E/AndroidRuntime(8036): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-28 15:43:36.534: E/AndroidRuntime(8036): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
04-28 15:43:36.534: E/AndroidRuntime(8036): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-28 15:43:36.534: E/AndroidRuntime(8036): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-28 15:43:36.534: E/AndroidRuntime(8036): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-28 15:43:36.534: E/AndroidRuntime(8036): at com.mahavega.qcdemo.JSONParser.makeHttpRequest(JSONParser.java:62)
04-28 15:43:36.534: E/AndroidRuntime(8036): at com.mahavega.qcdemo.MainActivity$GetEventDetails$1.run(MainActivity.java:198)
04-28 15:43:36.534: E/AndroidRuntime(8036): at android.os.Handler.handleCallback(Handler.java:615)
04-28 15:43:36.534: E/AndroidRuntime(8036): at android.os.Handler.dispatchMessage(Handler.java:92)
04-28 15:43:36.534: E/AndroidRuntime(8036): at android.os.Looper.loop(Looper.java:137)
04-28 15:43:36.534: E/AndroidRuntime(8036): at android.app.ActivityThread.main(ActivityThread.java:4856)
04-28 15:43:36.534: E/AndroidRuntime(8036): at java.lang.reflect.Method.invokeNative(Native Method)
04-28 15:43:36.534: E/AndroidRuntime(8036): at java.lang.reflect.Method.invoke(Method.java:511)
04-28 15:43:36.534: E/AndroidRuntime(8036): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
04-28 15:43:36.534: E/AndroidRuntime(8036): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
04-28 15:43:36.534: E/AndroidRuntime(8036): at dalvik.system.NativeStart.main(Native Method)