0

我是一名初学者 Android 程序员,我正在开发一个程序,该程序从本机设备电话簿中读取联系人并将其写入数据库 ( )。

我可以从我的手机获取和读取联系人,但我被困在“将联系人写入数据库”。这是我必须遵循的准则:

write function: 
write: 
[{'activity':'writeData', 
  'firstname':'Janis', 
  'lastname':'Berzins', 
  'telnr': '12312312'}]

request should be sent as: [{'activity':'readData'}]

example for read: [{"firstname":"Vards","lastname":"Uzvards","telnr":"12345678"},{"firstname":"Viesturs","lastname":"Lapsa","telnr":"11223344"}]

我研究了无数教程、文档等,花了四天时间,这就是我认为应该用于发送到数据库部分的内容:

public static HttpResponse doPost(String url, JSONObject c) throws ClientProtocolException, IOException 
{
    HttpParams httpParams = new BasicHttpParams();
    HttpClient httpclient = new DefaultHttpClient(httpParams);
    HttpPost request = new HttpPost(url);
    StringEntity s = new StringEntity(c.toString());
    s.setContentEncoding("UTF-8");
    s.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));

    request.setEntity(s);
    request.addHeader("accept", "application/json");

    return httpclient.execute(request);
}

为了创建一个简单的测试 Json,我使用了这个类:

    public class RestPost extends AsyncTask<String, Void, String>
{
    @Override
    protected String doInBackground(String... params) {
        JSONObject json = new JSONObject();

        try
        {
            json.put("activity", "writeData");
            json.put("firstname", "Janis");
            json.put("lastname", "Berzins");
            json.put("telnr", "123123123");

            RestMethods.doPost(Main.server, json);
        }

        catch (JSONException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.i("Error: ", e.toString());
        }
        catch (ClientProtocolException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.i("Error: ", e.toString());
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.i("Error: ", e.toString());
        }
        Log.i("DONE ", " I GUES");
        return null;
    }
}

但是在我的 Android 应用程序执行此功能后 - 数据库 () 中没有任何变化。

所以,拜托,谁能帮我弄清楚,我做错了什么?

谢谢!

4

2 回答 2

0

您需要一个 json 数组(请参阅规范中 json 周围的“[”和“]”字符。

    JSONArray jsonArray = new JSONArray();
    // your code to create the JSONObject
    // ...
    jsonArray.put(json);

    // use this within your RestMethods call
    RestMethods.doPost(Main.server, jsonArray);

您可能可以一次将多个对象发送到服务器 - 即使“规范”没有提到它,但使用数组是一个提示,您可以。

于 2013-04-13T19:49:28.843 回答
0

好的,我可以将它发送到数据库!:) 现在我正在使用 Get 方法.. 这就是我得到的 - 现在它似乎不起作用,可能是因为数据库中的数据是作为 JSONArray 发送的?

public static JSONObject doGet(String url)
{
    JSONObject json = null;

    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(url); 

    httpget.addHeader("accept", "application/json");
    HttpResponse response;

    try {
        response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();    

        if (entity != null) 
        {
            InputStream instream = entity.getContent();
            String result= convertStreamToString(instream);
            json=new JSONObject(result);     
            instream.close();
        }   
    } 

    catch (ClientProtocolException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.i("Error: ", e.toString());
    } 
    catch (IOException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.i("Error: ", e.toString());
    } 

    catch (JSONException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.i("Error: ", e.toString());
    }        

    return json;
}

public static 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();
        Log.i("Error: ", e.toString());
    } 
    finally 
    {
        try 
        {
            is.close();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
    return sb.toString();
} 
于 2013-04-15T17:35:59.237 回答