Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android jsonparser给定错误应用程序不插入数据_Android_Json - Fatal编程技术网

Android jsonparser给定错误应用程序不插入数据

Android jsonparser给定错误应用程序不插入数据,android,json,Android,Json,我正在学习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.a

我正在学习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)
对不起,我忘了给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);
}

?>

原因:java.lang.NullPointerException10-25 15:16:14.516
1996-2310/com.example.ahmed.json E/AndroidRuntime:at
MainActivity$InsertNewIdiom.doInBackground(MainActivity.java:76)

确保
json!=在使用它之前为空

因为
jsonParser.makeHttpRequest(url\u insert\u new,“GET”,params)
可以返回
null

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)

jsonParser.makeHttpRequest(url\u insert\u new,“GET”,params)返回
null

可能是本地url
http://10.0.2.2/idiomjson/insertnew.php
是exmaple,对您无效。

我应该为本地数据库的默认值插入什么?我已经插入了值127.0.0.1,但仍然会给出相同的错误谢谢您的帮助,先生,请指导我该怎么做,我对此知之甚少先生,你是对的,它的值为null,我在if-else条件下检查它,if(json!=null){do work}else{Toast.makeText(getApplicationContext(),“json值为null”,Toast.LENGTH_LONG.show();},它显示值为null,现在它给出以下错误E/AndroidRuntime:at.JSONParser.makeHttpRequest(JSONParser.java:47)E/AndroidRuntime:at.MainActivity$InsertNewIdiom.doInBackground(MainActivity.java:75)E/AndroidRuntime:at.MainActivity.onClick(MainActivity.java:47)发布完整的logcat后,我们可以从这一部分找出错误的来源。当我尝试使用它时,它给出json null的值,它给出了新logcat中的错误
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;
}
    }
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
 //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);
}

?>