使用JSON在服务器中插入数据

时间:2015-06-24 05:41:46

标签: android json

我正在尝试使用JSON从Android应用程序在服务器中插入数据。但我收到IP地址错误。谁能告诉我我的代码中有什么问题?我正在上传我的代码,其中插入操作已完成。它只是提供Toast - 无效的IP地址。但我的IP地址无效。

public void insert()
{
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

    nameValuePairs.add(new BasicNameValuePair("id",id));
    nameValuePairs.add(new BasicNameValuePair("name",name));

    try
    {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://192.168.1.145/tracker/index.php/json/add_task");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
        Log.e("pass 1", "connection success ");
    }
    catch(Exception e)
    {
        Log.e("Fail 1", e.toString());
        Toast.makeText(getApplicationContext(), "Invalid IP Address",
                Toast.LENGTH_LONG).show();
    }

    try
    {
        BufferedReader reader = new BufferedReader
                (new InputStreamReader(is,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        while ((line = reader.readLine()) != null)
        {
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();
        Log.e("pass 2", "connection success ");
    }
    catch(Exception e)
    {
        Log.e("Fail 2", e.toString());
    }

    try
    {
        JSONObject json_data = new JSONObject(result);
        code=(json_data.getInt("code"));

        if(code==1)
        {
            Toast.makeText(getBaseContext(), "Inserted Successfully",
                    Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(getBaseContext(), "Sorry, Try Again",
                    Toast.LENGTH_LONG).show();
        }
    }
    catch(Exception e)
    {
        Log.e("Fail 3", e.toString());
    }
}

我们非常感谢您的及时回复。谢谢。

2 个答案:

答案 0 :(得分:0)

使用此功能并检查其是否正常工作

public String request(String url, List<NameValuePair> nameValuePairs)
{

    try
    {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        // httpPost.setHeader("encytype", "multipart/form-data");

        HttpParams httpParameters = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8);
        httpPost.setEntity(entity);

        HttpResponse httpResponse = httpClient.execute(httpPost);

        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");
        }
        reader.close();
        json = sb.toString();
    }
    catch (Exception e)
    {
        Log.e("log_tag", "Buffer Error" + "Error converting result " + e.toString());
    }

    return json;
}

答案 1 :(得分:0)

感谢大家的回复。我在$('.menu > li').hover(function() { // Show submenu $(this).find('> ul.submenu').show(); }, function() { // Hide submenu $(this).find('> ul.submenu').hide(); }); 下添加了以下代码,这解决了我的IP地址问题。

setContentView(R.layout.activity_insert);

干杯!