Android使用JSON插入mysql

时间:2015-03-12 13:53:55

标签: php android mysql json

我发现以下错误:

03-12 21:47:19.878  13513-13513/com.example.frost.mysqlinsert W/System.err﹕ org.json.JSONException: End of input at character 0 of
03-12 21:47:19.878  13513-13513/com.example.frost.mysqlinsert W/System.err﹕ at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
03-12 21:47:19.888  13513-13513/com.example.frost.mysqlinsert W/System.err﹕ at org.json.JSONTokener.nextValue(JSONTokener.java:97

这是我的代码:

public class MainActivity extends ActionBarActivity implements View.OnClickListener
{

    public EditText uName;
    public EditText uPass;
    public EditText uPermi;
    public Button uBtn;


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

        uName = (EditText) findViewById(R.id.et1);
        uPass = (EditText) findViewById(R.id.et2);
        uPermi = (EditText) findViewById(R.id.et3);
        uBtn = (Button) findViewById(R.id.btn);
        uBtn.setOnClickListener(this);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().build();
        StrictMode.setThreadPolicy(policy);
    }

    @Override
    public void onClick(View v)
    {
        if (v.getId() == R.id.btn)
        {
            SaveData();
        }
    }

    public boolean SaveData()
    {
        final AlertDialog.Builder ad = new AlertDialog.Builder(this);
        ad.setTitle("Error!");
        ad.setIcon(android.R.drawable.btn_star);
        ad.setPositiveButton("Close", null);

        if (uName.getText().length() == 0)
        {
            ad.setMessage("Please input username");
            ad.show();
            uName.requestFocus();
            return false;
        }

        if (uPass.getText().length() == 0)
        {
            ad.setMessage("Please input password");
            ad.show();
            uName.requestFocus();
            return false;
        }

        if (uPermi.getText().length() == 0)
        {
            ad.setMessage("Please input pemission");
            ad.show();
            uName.requestFocus();
            return false;
        }

        String url = "http://www.php-orgil.rhcloud.com/insert_android.php";
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("uname", uName.getText().toString()));
        params.add(new BasicNameValuePair("upass", uPass.getText().toString()));
        params.add(new BasicNameValuePair("uper", uPermi.getText().toString()));

        String resultServer = getHttpPost(url, params);

        String strStatusID = "0";
        String strError = "Unknown status";

        JSONObject c;

        try
        {
            c = new JSONObject(resultServer);
            strStatusID = c.getString("StatusID");
            strError = c.getString("Error");
            Toast.makeText(getApplicationContext(),"json awla",Toast.LENGTH_LONG).show();
        }

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

        if (strStatusID.equals("0"))
        {
            ad.setMessage(strError+"!!!");
            ad.show();
        }
        else
        {
            Toast.makeText(getApplicationContext(), "Save Data Successfully",Toast.LENGTH_LONG).show();
            uName.setText("");
            uPass.setText("");
            uPermi.setText("");
        }

        return true;
    }

    public String getHttpPost(String url, List<NameValuePair> params)
    {
        StringBuilder sb = new StringBuilder();
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        try
        {
            httpPost.setEntity(new UrlEncodedFormEntity(params));
            HttpResponse httpResponse = httpClient.execute(httpPost);
            StatusLine statusline = httpResponse.getStatusLine();
            int StatusCode = statusline.getStatusCode();

            if (StatusCode == 200)
            {
                HttpEntity entity = httpResponse.getEntity();
                InputStream content = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(content));
                String line;

                while ((line = reader.readLine()) != null)
                {
                    sb.append(line+"\n");
                }
            }
            else
            {
                Log.e("Log","Failed to download result");
            }
        }
        catch (Exception e)
        {

        }

        return sb.toString();
    }

}

1 个答案:

答案 0 :(得分:0)

One of the Situation我们面临"org.json.JSONException: End of input at character 0 of"该网页无法使用。或者返回空白页面。

在您的情况下,特定链接&#34; http://www.php-orgil.rhcloud.com/insert_android.php&#34;也没有网页。

我只是更改了网址&#34; http://php-orgil.rhcloud.com/insert_android.php&#34; 像这样,它的工作原理。试试吧 这是建议之一

注意:只需删除www.

即可