数据未从android

时间:2016-04-26 17:50:30

标签: android json

我的数据没有插入到webhost的数据库中。

这是我的ActionActivity.java,我将把数据发送到数据库。

import android.app.Activity;
import android.content.Context;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.InputStream;


public class ActionActivity extends Activity {
    private String t;
    TextView timer;
    EditText e1,e2;
    Button save;
    static InputStream is = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_action);
        Bundle time = getIntent().getExtras();
        t = (String) time.get("com.example.masba.timemanagement.MESSAGE");
        e1 = (EditText) findViewById(R.id.editText);
        e1.setText(t, TextView.BufferType.EDITABLE);
        e2 = (EditText) findViewById(R.id.editText2);
        save = (Button) findViewById(R.id.save);
    }


    public void insert(View view){
        String actionName = e1.getText().toString();
        String time = e2.getText().toString();
        Toast.makeText(this, "Data Inserting...", Toast.LENGTH_SHORT).show();
        new SignupActivity(this).execute(actionName, time);
        e1.setText("");
        e2.setText("");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

现在我的SignupActivity.java扩展了Asynctask。

import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class SignupActivity extends AsyncTask<String, Void, String> {

    private Context context;

    public SignupActivity(Context context) {
        this.context = context;
    }

    protected void onPreExecute() {

    }

    @Override
    protected String doInBackground(String... arg0) {
        String actionName = arg0[0];
        String time = arg0[1];


        String link;
        String data;
        BufferedReader bufferedReader;
        String result;
        try {
            data = "?actioname=" + URLEncoder.encode(actionName, "UTF-8");

            data += "&time=" + URLEncoder.encode(time, "UTF-8");
            link = "http://masumalmasba.comli.com/insertdata.php" + data;
            URL url = new URL(link);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();

            bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
            result = bufferedReader.readLine();
            return result;
        } catch (Exception e) {

            return new String("Exception: " + e.getMessage());
        }
    }

    @Override
    protected void onPostExecute(String result) {
        String jsonStr = result;
        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);
                String query_result = jsonObj.getString("query_result");
                if (query_result.equals("SUCCESS")) {
                    Toast.makeText(context, "Data inserted successfully.", Toast.LENGTH_SHORT).show();
                } else if (query_result.equals("FAILURE")) {
                    Toast.makeText(context, "Data could not be inserted.", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(context, "Couldn't connect to remote database.", Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(context, "Error parsing JSON data.", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(context, "Couldn't get any JSON data.", Toast.LENGTH_SHORT).show();
        }
    }
}

现在我的insertdata.php代码是:

<?php

$servername="mysql6.000webhost.com";
$username = "a1871724_masum";
$password = "almasba012";
$dbname = "a1871724_actiont";

$conn = new mysqli($servername , $username , $password , $dbname);
if($conn->connect_error)
{
    die("Connection Failed: ".$conn->connect_error);
}

$action = $_GET['actionname'];
$time = $_GET['time'];
$sql = "INSERT INTO actiontime (name,time) VALUES ('($action)','($time)')";

if($conn->query($sql)===TRUE){
echo "New record created succesfully";
}
else{
echo "Error";
mysql_close($conn);
}

?>

0 个答案:

没有答案