dongtiao2976 2015-03-11 15:58
浏览 246

错误org.json.JSONException

I am creating an application for Android. It is the task of retrieving data from an external database server mysql. Everything was fine, but by the time of compilation. Gave me the error:

Errororg.json.JSONException: Value

A link to a database using php script:

$host="localhost";
$username="root";
$password="password";
$db_name="baza";

$con=mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql = "select * from zastep";
$result = mysql_query($sql);
$json = array();

if(mysql_num_rows($result)){
while($row=mysql_fetch_assoc($result)){
$json['zastep'][]=$row;
}
}
mysql_close($con);
echo json_encode($json);
?>

The script is working correctly, the error shows up when compiling apk.

 import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
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 java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MainActivity extends Activity{
    private String jsonResult;
    private String url = "http://192.168.2.100/tyr/conn.php";
    private ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.listView);
        accessWebService();
    }
     @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    //uzyskiwanie dostępu do sieci
    private class JsonReadTask extends AsyncTask<String,Void, String> {
        @Override
        protected String doInBackground(String... params)
        {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(params[0]);
            try
            {
                HttpResponse response = httpclient.execute(httppost);
                jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return null;
        }
        private StringBuilder inputStreamToString(InputStream is)
        {
            String rLine = "";
            StringBuilder answer = new StringBuilder();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            try {
                while ((rLine = rd.readLine())!= null)
                {
                    answer.append(rLine);
                }
            } catch (IOException e) {
                Toast.makeText(getApplicationContext(), "Error..." + e.toString(), Toast.LENGTH_LONG).show();
            }
            return answer;
        }
        @Override
        protected void onPostExecute(String result)
        {
            ListDrwaer();
        }
    }
    public void accessWebService()
    {
        JsonReadTask task = new JsonReadTask();
        task.execute(new String[] {url});
    }
    public void ListDrwaer()
    {
        List<Map<String, String>> employeeList = new ArrayList<Map<String, String>>();
        try
        {
            JSONObject jsonResponse = new JSONObject(jsonResult);
            JSONArray jsonMainNode = jsonResponse.optJSONArray("zastep");
            for (int i = 0; i < jsonMainNode.length(); i++)
            {
                JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
                String name = jsonChildNode.optString("klasa");
                String data = jsonChildNode.optString("data");
                String outPut = name + "-" + data;
                employeeList.add(createEmployee("employees", outPut));
            }
        }
        catch (JSONException e)
        {
            Toast.makeText(getApplicationContext(), "Error" + e.toString(),Toast.LENGTH_SHORT).show();
        }
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, employeeList,android.R.layout.simple_list_item_1, new String[]{"employees"}, new int[] {android.R.id.text1});
        listView.setAdapter(simpleAdapter);
    }
    private HashMap<String, String> createEmployee(String name, String data)
    {
        HashMap<String, String> employeeNameNo = new HashMap<String,String>();
        employeeNameNo.put(name, data);
        return employeeNameNo;
    }

}

Logcat:

03-11 17:26:07.790  26957-26957/pl.saperdak1gmail.rrt E/﹕ appName=pl.saperdak1gmail.rrt, acAppName=/system/bin/surfaceflinger
03-11 17:26:07.790  26957-26957/pl.saperdak1gmail.rrt E/﹕ 0

please help

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?