我正在研究json解析器,我正在通过观看演示制作应用程序 我按照他们在演示中说的一行一行完成,但我的应用程序不起作用 我的问题的答案不在该网站上,请提前感谢指导我
我的日志猫是
10-25 15:16:05.766 1996-1996/com.example.ahmed.json E/Trace: error opening trace file: No such file or directory (2)
10-25 15:16:14.484 1996-2310/com.example.ahmed.json E/Buffer Error: Error converting result java.lang.NullPointerException
10-25 15:16:14.484 1996-2310/com.example.ahmed.json E/JSON Parser: Error parsing data org.json.JSONException: End of input at character 0 of
10-25 15:16:14.516 1996-2310/com.example.ahmed.json E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
10-25 15:16:14.516 1996-2310/com.example.ahmed.json E/AndroidRuntime: java.lang.RuntimeException: An error occured while executing doInBackground()
10-25 15:16:14.516 1996-2310/com.example.ahmed.json E/AndroidRuntime: at android.os.AsyncTask$3.done(AsyncTask.java:299)
10-25 15:16:14.516 1996-2310/com.example.ahmed.json E/AndroidRuntime: at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
10-25 15:16:14.516 1996-2310/com.example.ahmed.json E/AndroidRuntime: at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
10-25 15:16:14.516 1996-2310/com.example.ahmed.json E/AndroidRuntime: at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
10-25 15:16:14.516 1996-2310/com.example.ahmed.json E/AndroidRuntime: at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-25 15:16:14.516 1996-2310/com.example.ahmed.json E/AndroidRuntime: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-25 15:16:14.516 1996-2310/com.example.ahmed.json E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-25 15:16:14.516 1996-2310/com.example.ahmed.json E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-25 15:16:14.516 1996-2310/com.example.ahmed.json E/AndroidRuntime: at java.lang.Thread.run(Thread.java:856)
10-25 15:16:14.516 1996-2310/com.example.ahmed.json E/AndroidRuntime: Caused by: java.lang.NullPointerException
10-25 15:16:14.516 1996-2310/com.example.ahmed.json E/AndroidRuntime: at com.example.ahmed.json.MainActivity$InsertNewIdiom.doInBackground(MainActivity.java:76)
10-25 15:16:14.516 1996-2310/com.example.ahmed.json E/AndroidRuntime: at com.example.ahmed.json.MainActivity$InsertNewIdiom.doInBackground(MainActivity.java:57)
10-25 15:16:14.516 1996-2310/com.example.ahmed.json E/AndroidRuntime: at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-25 15:16:14.516 1996-2310/com.example.ahmed.json E/AndroidRuntime: at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-25 15:16:14.516 1996-2310/com.example.ahmed.json E/AndroidRuntime: at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-25 15:16:14.516 1996-2310/com.example.ahmed.json E/AndroidRuntime: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-25 15:16:14.516 1996-2310/com.example.ahmed.json E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-25 15:16:14.516 1996-2310/com.example.ahmed.json E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-25 15:16:14.516 1996-2310/com.example.ahmed.json E/AndroidRuntime: at java.lang.Thread.run(Thread.java:856)
10-25 15:16:15.680 1996-1996/com.example.ahmed.json E/WindowManager: Activity com.example.ahmed.json.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41972358 that was originally added here
10-25 15:16:15.680 1996-1996/com.example.ahmed.json E/WindowManager: android.view.WindowLeaked: Activity com.example.ahmed.json.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41972358 that was originally added here
10-25 15:16:15.680 1996-1996/com.example.ahmed.json E/WindowManager: at android.view.ViewRootImpl.<init>(ViewRootImpl.java:382)
10-25 15:16:15.680 1996-1996/com.example.ahmed.json E/WindowManager: at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292)
10-25 15:16:15.680 1996-1996/com.example.ahmed.json E/WindowManager: at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
10-25 15:16:15.680 1996-1996/com.example.ahmed.json E/WindowManager: at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
10-25 15:16:15.680 1996-1996/com.example.ahmed.json E/WindowManager: at android.view.Window$LocalWindowManager.addView(Window.java:547)
10-25 15:16:15.680 1996-1996/com.example.ahmed.json E/WindowManager: at android.app.Dialog.show(Dialog.java:284)
10-25 15:16:15.680 1996-1996/com.example.ahmed.json E/WindowManager: at com.example.ahmed.json.MainActivity$InsertNewIdiom.onPreExecute(MainActivity.java:68)
10-25 15:16:15.680 1996-1996/com.example.ahmed.json E/WindowManager: at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
10-25 15:16:15.680 1996-1996/com.example.ahmed.json E/WindowManager: at android.os.AsyncTask.execute(AsyncTask.java:534)
10-25 15:16:15.680 1996-1996/com.example.ahmed.json E/WindowManager: at com.example.ahmed.json.MainActivity.onClick(MainActivity.java:47)
10-25 15:16:15.680 1996-1996/com.example.ahmed.json E/WindowManager: at android.view.View.performClick(View.java:4106)
10-25 15:16:15.680 1996-1996/com.example.ahmed.json E/WindowManager: at android.view.View$PerformClick.run(View.java:17052)
10-25 15:16:15.680 1996-1996/com.example.ahmed.json E/WindowManager: at android.os.Handler.handleCallback(Handler.java:615)
10-25 15:16:15.680 1996-1996/com.example.ahmed.json E/WindowManager: at android.os.Handler.dispatchMessage(Handler.java:92)
10-25 15:16:15.680 1996-1996/com.example.ahmed.json E/WindowManager: at android.os.Looper.loop(Looper.java:137)
10-25 15:16:15.680 1996-1996/com.example.ahmed.json E/WindowManager: at android.app.ActivityThread.main(ActivityThread.java:5059)
10-25 15:16:15.680 1996-1996/com.example.ahmed.json E/WindowManager: at java.lang.reflect.Method.invokeNative(Native Method)
10-25 15:16:15.680 1996-1996/com.example.ahmed.json E/WindowManager: at java.lang.reflect.Method.invoke(Method.java:511)
10-25 15:16:15.680 1996-1996/com.example.ahmed.json E/WindowManager: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
10-25 15:16:15.680 1996-1996/com.example.ahmed.json E/WindowManager: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
10-25 15:16:15.680 1996-1996/com.example.ahmed.json E/WindowManager: at dalvik.system.NativeStart.main(Native Method)
我的主要活动是
package com.example.ahmed.json;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity
implements OnClickListener {
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
EditText txtnewidiom;
EditText txtmeaning;
private Button btnsavenew;
private int success;
private static String url_insert_new = "http://10.0.2.2/idiomjson/insertnew.php";
private static final String TAG_SUCCESS = "success";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtnewidiom = (EditText) findViewById(R.id.txtnewidiom);
txtmeaning = (EditText) findViewById(R.id.txtmeaning);
btnsavenew = (Button) findViewById(R.id.btnsavenew);
btnsavenew.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if
(v.getId() == R.id.btnsavenew) {
new InsertNewIdiom().execute();
if (success == 1) {
Toast.makeText(getApplicationContext(), "New idiom saved...", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "New idiom FAILED to saved...", Toast.LENGTH_LONG).show();
}
}
}
class InsertNewIdiom extends AsyncTask<String, String, String> {
String entry = txtnewidiom.getText().toString();
String meaning = txtmeaning.getText().toString();
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Saving the new IDIOM (" + entry + ")...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("entry", entry));
params.add(new BasicNameValuePair("meaning", meaning));
JSONObject json = jsonParser.makeHttpRequest(url_insert_new, "GET", params);
Log.d("Insert New Idiom Response", json.toString());
try {
success = json.getInt(TAG_SUCCESS);
if (success == 1) { // successfully save new idiom
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
}
}
}
json解析器的类是
package com.example.ahmed.json;
import android.util.Log;
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 java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public JSONParser() {
}
public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params)
{ try
{
if(method == "POST")
{
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"){
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 {
jObj = new JSONObject(json);
}
catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}
}
在json上尝试if else后会出现此错误
10-29 00:48:14.446 29434-29434/com.example.ahmed.json E/Trace: error opening trace file: No such file or directory (2)
10-29 00:48:39.017 29434-29434/com.example.ahmed.json E/AndroidRuntime: FATAL EXCEPTION: main
10-29 00:48:39.017 29434-29434/com.example.ahmed.json E/AndroidRuntime: android.os.NetworkOnMainThreadException
10-29 00:48:39.017 29434-29434/com.example.ahmed.json E/AndroidRuntime: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1131)
10-29 00:48:39.017 29434-29434/com.example.ahmed.json E/AndroidRuntime: at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
10-29 00:48:39.017 29434-29434/com.example.ahmed.json E/AndroidRuntime: at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
10-29 00:48:39.017 29434-29434/com.example.ahmed.json E/AndroidRuntime: at libcore.io.IoBridge.connect(IoBridge.java:112)
10-29 00:48:39.017 29434-29434/com.example.ahmed.json E/AndroidRuntime: at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
10-29 00:48:39.017 29434-29434/com.example.ahmed.json E/AndroidRuntime: at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:460)
10-29 00:48:39.017 29434-29434/com.example.ahmed.json E/AndroidRuntime: at java.net.Socket.connect(Socket.java:832)
10-29 00:48:39.017 29434-29434/com.example.ahmed.json E/AndroidRuntime: at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
10-29 00:48:39.017 29434-29434/com.example.ahmed.json E/AndroidRuntime: at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
10-29 00:48:39.017 29434-29434/com.example.ahmed.json E/AndroidRuntime: at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-29 00:48:39.017 29434-29434/com.example.ahmed.json E/AndroidRuntime: at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-29 00:48:39.017 29434-29434/com.example.ahmed.json E/AndroidRuntime: at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
10-29 00:48:39.017 29434-29434/com.example.ahmed.json E/AndroidRuntime: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-29 00:48:39.017 29434-29434/com.example.ahmed.json E/AndroidRuntime: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-29 00:48:39.017 29434-29434/com.example.ahmed.json E/AndroidRuntime: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-29 00:48:39.017 29434-29434/com.example.ahmed.json E/AndroidRuntime: at com.example.ahmed.json.JSONParser.makeHttpRequest(JSONParser.java:47)
10-29 00:48:39.017 29434-29434/com.example.ahmed.json E/AndroidRuntime: at com.example.ahmed.json.MainActivity$InsertNewIdiom.doInBackground(MainActivity.java:75)
10-29 00:48:39.017 29434-29434/com.example.ahmed.json E/AndroidRuntime: at com.example.ahmed.json.MainActivity.onClick(MainActivity.java:47)
10-29 00:48:39.017 29434-29434/com.example.ahmed.json E/AndroidRuntime: at android.view.View.performClick(View.java:4106)
10-29 00:48:39.017 29434-29434/com.example.ahmed.json E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:17052)
10-29 00:48:39.017 29434-29434/com.example.ahmed.json E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:615)
10-29 00:48:39.017 29434-29434/com.example.ahmed.json E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:92)
10-29 00:48:39.017 29434-29434/com.example.ahmed.json E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
10-29 00:48:39.017 29434-29434/com.example.ahmed.json E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5059)
10-29 00:48:39.017 29434-29434/com.example.ahmed.json E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
10-29 00:48:39.017 29434-29434/com.example.ahmed.json E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
10-29 00:48:39.017 29434-29434/com.example.ahmed.json E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
10-29 00:48:39.017 29434-29434/com.example.ahmed.json E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
10-29 00:48:39.017 29434-29434/com.example.ahmed.json E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
抱歉,我忘了给php文件
<?php
//insertnew.php /* * Following code will create a new product row * All product details are read from HTTP GET Request */ // array for JSON response
$response = array();
// check for required fields
if (isset($_GET['entry']) && isset($_GET['meaning'])) {
$entry = $_GET['entry'];
$meaning = $_GET['meaning'];
// include db connect class
require_once __DIR__ . '/connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row (idioms)
$result = mysql_query("INSERT INTO idiomlist(entry, meaning) VALUES('$entry', '$meaning')");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1; $response["message"] = "new IDIOM saved....";
// echoing JSON response
echo json_encode($response);
}
else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
}
else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
答案 0 :(得分:0)
引起:java.lang。 NullPointerException 10-25 15:16:14.516 1996-2310 / com.example.ahmed.json E / AndroidRuntime:at com.example.ahmed.json.MainActivity $ InsertNewIdiom.doInBackground(MainActivity.java:76)
使用前请确保json != null
。
因为jsonParser.makeHttpRequest(url_insert_new, "GET", params);
可以返回null
值。
答案 1 :(得分:0)
10-25 15:16:14.516 1996-2310 / com.example.ahmed.json E / AndroidRuntime:引起:java.lang.NullPointerException 10-25 15:16:14.516 1996-2310 / com.example.ahmed.json E / AndroidRuntime:at com.example.ahmed.json.MainActivity $ InsertNewIdiom.doInBackground(MainActivity.java:76) 10-25 15:16:14.516 1996-2310 / com.example.ahmed.json E / AndroidRuntime:at com.example.ahmed.json.MainActivity $ InsertNewIdiom.doInBackground(MainActivity.java:57) 10-25 15:16:14.516 1996-2310 / com.example.ahmed.json E / AndroidRuntime:at android.os.AsyncTask $ 2.call(AsyncTask.java:287)
l = 6
z = 0
while z < l:
print(z)
z = z + 1
l = 10
返回jsonParser.makeHttpRequest(url_insert_new, "GET", params);
。
也许这个本地网址null
是一个例子,对你无效。