php - Android使用JSON插入mysql

标签 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();
    }

}

最佳答案

我们面临的情况之一“org.json.JSONException:在字符 0 处输入结束” 是网页不可用。或者返回空页。

在您的情况下,特定链接“http://www.php-orgil.rhcloud.com/insert_android.php”也无法使用网页。

我只是更改了网址“http://php-orgil.rhcloud.com/insert_android.php” 像这样并且它有效。尝试一下 这是建议之一

注意:只需删除www.

关于php - Android使用JSON插入mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29011707/

相关文章:

php - 使用 docker、nginx、php-fpm 提供静态文件

php 在运行时更改脚本

php - Laravel 获取 sql 查询的总和

php - 单击提交按钮时隐藏隐藏的输入值 (php/jquery)

php - 在侧边栏中显示文件夹和子文件夹

java - 没有标题的 DialogFragment 缩小对话框宽度

android - 将应用程序发布到 google play - 演示帐户数据在哪里?

Android 从 Webview 保存图像并启用 javascript

Mysql:插入时备份?

php - 计算另一行中的条目数