Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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 Json unicode parsin_Android_Json_Unicode - Fatal编程技术网

Android Json unicode parsin

Android Json unicode parsin,android,json,unicode,Android,Json,Unicode,我的sql server排序规则是utf8 unicode 我的php工作得很好 我的android项目可以正常使用普通字符,但如果是阿拉伯文字符,在下面列出的活动中解析json行233时会显示错误 我已经在其他项目上尝试了我的sql和php,但在这里我不知道错误是什么,json必须是utf8 公共类DealsStactivity扩展了活动{ private String id; // Progress Dialog private ProgressDialog pDialog; // cr

我的sql server排序规则是utf8 unicode 我的php工作得很好 我的android项目可以正常使用普通字符,但如果是阿拉伯文字符,在下面列出的活动中解析json行233时会显示错误 我已经在其他项目上尝试了我的sql和php,但在这里我不知道错误是什么,json必须是utf8

公共类DealsStactivity扩展了活动{

private String id;

// Progress Dialog
private ProgressDialog pDialog;

// creating JSON Parser object
JSONParser jParser = new JSONParser();


private static String url = "http://xxxxxxxxxxxx.get_all_deals_by_id.php";

// JSON Node names
private static final String TAG_DEALS = "deals";
private static final String TAG_ID = "id";
private static final String TAG_DEALNAME = "dealName";
private static final String TAG_PRICE = "price";
private static final String TAG_DESCRIPTION = "description";
private static final String TAG_RESTID = "restID";
private static final String TAG_RESTNAME = "restName";
private static final String TAG_RESTTYPE = "restType";
private static final String TAG_LAT = "restLat";
private static final String TAG_LNG = "restLng";
private static final String TAG_SUCCESS = "success";

private JSONObject json;

JSONArray restaurantDealsData = null;

// Hashmap for ListView
ArrayList<HashMap<String, String>> restaurantDealsList = new ArrayList<HashMap<String, String>>();

ListView myList;

private String[] dealID;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_deals_list);

    id = getIntent().getStringExtra("id");

    myList = (ListView) findViewById(R.id. restDealListView);

    new LoadDeals().execute();
}

/**
 * Background Async Task to Load all deals by making
 * HTTP Request
 * */
class LoadDeals extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(DealsListActivity.this);
        pDialog.setMessage("Loading Restaurant Deals. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        Log.v("id", id);
        params.add(new BasicNameValuePair("id", id));

        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url, "GET", params);
        // check log cat for JSON string from URL
        Log.v("restaurantDealsJSON: ", json.toString());

        // return json as string to using in the user interface
        return json.toString();
    }

    @Override
    protected void onPostExecute(final String jsonStr) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();

        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                /**
                 * Updating parsed JSON data into listview
                 * */
                try {
                    json = new JSONObject(jsonStr);
                } catch (JSONException e1) {
                    // print error message to log
                    e1.printStackTrace();

                    error("There are no Deals");

                }

                try {
                    // Checking for SUCCES TAG
                    int success = json.getInt(TAG_SUCCESS);

                    if (success == 1) {
                        // restaurant found


                        // Getting Array of restaurant

                        restaurantDealsData = json.getJSONArray(TAG_DEALS);
                        displayDeals(restaurantDealsData.toString());
                    } else {

                        error("There is no Deals available!");
                    }
                } catch (JSONException e) {
                    error("There has been an error please try again!");
                    e.printStackTrace();
                }
            }
        });
    }
}

public void error(String error) {
    // Log.v("ERROR", "2");
    AlertDialog.Builder builder = new AlertDialog.Builder(
            DealsListActivity.this);
    // Log.v("ERROR", "3");
    builder.setTitle("Error");
    builder.setMessage(error);
    builder.setCancelable(false);
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {
            // Log.v("TEST", "1");
            Intent i = new Intent(getApplicationContext(),
                    TabsViewPagerFragmentActivity.class);
            startActivity(i);
            overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
            finish();

        }
    });
    AlertDialog alert = builder.create();
    alert.show();
}

public void displayDeals(String result) {
    JSONArray restaurantDealsData = null;

    try {
        restaurantDealsList.clear();

        restaurantDealsData = new JSONArray(result);

        dealID = new String[restaurantDealsData.length()];
        // looping through all technical data
        for (int i = 0; i < restaurantDealsData.length(); i++) {
            JSONObject td = restaurantDealsData.getJSONObject(i);

            // Storing each json item in variable
            String id = td.getString(TAG_ID);
            dealID[i] = id;

            String name = td.getString(TAG_DEALNAME);
            String price = td.getString(TAG_PRICE);
            String description = td.getString(TAG_DESCRIPTION);
            String restaurantID = td.getString(TAG_RESTID);
            String restaurantName = td.getString(TAG_RESTNAME);
            String restaurantType = td.getString(TAG_RESTTYPE);
            String lat = td.getString(TAG_LAT);
            String lng = td.getString(TAG_LNG);

            Log.v("lat", lat);
            Log.v("lng", lng);

            // Creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();

            // adding each child node to HashMap key => value
            map.put(TAG_ID, id);
            map.put(TAG_DEALNAME, name);
            map.put(TAG_PRICE, price);
            map.put(TAG_DESCRIPTION, description);
            map.put(TAG_RESTID, restaurantID);
            map.put(TAG_RESTNAME, restaurantName);
            map.put(TAG_RESTTYPE, restaurantType);
            map.put(TAG_LAT, lat);
            map.put(TAG_LNG, lng);


            // adding HashMap to ArrayList
            restaurantDealsList.add(map);
        }

    } catch (JSONException e) {
        e.printStackTrace();
        error("Error parsing json");
    }

    // add to list view
    /**
     * Updating parsed JSON data into ListView
     * */
    ListAdapter adapter = new SimpleAdapter(getApplicationContext(),
            restaurantDealsList, R.layout.deals_list_item, new String[] {
                    TAG_DEALNAME, TAG_RESTNAME, TAG_RESTTYPE }, new int[] {
                    R.id.dealName, R.id.restaurantName, R.id.type });
    // updating listview
    myList.setAdapter(adapter);
    //handling user click list item
    myList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            //start the next activity - Restaurant Details
            Intent i = new Intent(getApplicationContext(), DealDetails.class);
            i.putExtra("ID", dealID[arg2]);
            startActivity(i);
            overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
        }

    });
}
}

公共类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 mehtod
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;

}

}

您应该使用字符集UTF-8读取输入流,如下所示:

替换

BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);


我用这个来解码里面有日语的json。

你能把你的JSONParser添加到问题中吗?@SaNtoRiaN是的,我刚刚添加了它现在检查一下解析器没有问题,活动中的233行是什么?displayingdeals活动的一部分Hi!为什么不为displayDeals设置JSONArray参数而不是字符串呢?还是一样的问题
 BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8));