数据库连接错误

时间:2017-02-04 13:38:40

标签: android json

enter code here

        public class HttpClient extends AsyncTask<Void, Void, JSONArray> {
            private final String TAG = "HttpClient";
            private String URL;
            private JSONObject jsonObjSend;
            private JSONArray result=null;

            public HttpClient(String URL, JSONObject jsonObjSend) {
                this.URL = URL;
                this.jsonObjSend = jsonObjSend;
            }

            public void calistir() {
                this.execute();            }

            @Override
            protected JSONArray doInBackground(Void... params) {

                try {
                    DefaultHttpClient httpclient = new DefaultHttpClient();
                    HttpPost httpPostRequest = new HttpPost(URL);

                    StringEntity se;
                    se = new StringEntity(jsonObjSend.toString());

                    // Set HTTP parameters
                    httpPostRequest.setEntity(se);
                    httpPostRequest.setHeader("Accept", "application/json");
                    httpPostRequest.setHeader("Content-type", "application/json");

                    long t = System.currentTimeMillis();
                    HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);

                    HttpEntity entity = response.getEntity();

                    if (entity != null) {
                        // Read the content stream
                        InputStream instream = entity.getContent();

                        // convert content stream to a String
                        String resultString = convertStreamToString(instream);
                        instream.close();


                        JSONArray jsonObjRecv = new JSONArray(resultString);

                        // Raw DEBUG output of our received JSON object:
                        Log.d(TAG, "<JSONObject>\n" + jsonObjRecv.toString() + "\n</JSONObject>");

                        return jsonObjRecv;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }

            protected void onPostExecute(JSONArray jObject) {
                result = jObject;
                for (int i=0;i<result.length();i++){
                    try {
                        JSONObject jsonObject = result.getJSONObject(i);
                        uyeler.add(jsonObject.getString("name") + " - " + jsonObject.getString("email")+ " - " + jsonObject.getString("no"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                ArrayAdapter arrayAdapter = new ArrayAdapter(getApplicationContext(),android.R.layout.simple_list_item_1,uyeler);
                lvUyeler.setAdapter(arrayAdapter);
                Toast.makeText(getApplicationContext(), "listelendi", Toast.LENGTH_SHORT).show();
                Log.d("JsonArray",result.toString());
            }

            private String convertStreamToString(InputStream is) {

                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                StringBuilder sb = new StringBuilder();

                String line = null;
                try {
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return sb.toString();
            }
        }

Logcet详情

                        String resultString  onvertStreamToString(instream);
                        instream.close();

Toast.makeText(getApplicationContext(),&#34;&#34; + resultString,Toast.LENGTH_LONG)。show(); //不连接数据库 JSONArray jsonObjRecv = new JSONArray(resultString);

不要连接数据库

  

块引用

0 个答案:

没有答案