package com.example.korisnik.test;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class DbManager extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
}
public void register(View v) {
EditText uname = (EditText) findViewById(R.id.username);
EditText pw = (EditText) findViewById(R.id.password);
String username = uname.getText().toString();
String password = pw.getText().toString();
System.out.println("Username is: " + username + " ,and password is: " + password);
Uploader task = new Uploader();
task.execute(new String[] { "http://10.0.2.2/user_db/senddata.php", username, password });
}
public class Uploader extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String response = null;
try {
response += postHttpContent(params[0],params[1],params[2]);
} catch (IOException e) {
System.out.println("IO Error");
Log.e("error", e.toString());
} catch (JSONException e) {
System.out.println("JSON Error");
e.printStackTrace();
}
return response;
}
@Override
protected void onPostExecute(String result) {
System.out.println(result);
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
public String postHttpContent(String urlStr, String user, String pass) throws IOException, JSONException {
String response = "";
URL url = new URL(urlStr);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
httpConn.setRequestMethod("POST");
JSONObject userdata = new JSONObject();
userdata.put("username",user);
userdata.put("password", pass);
JSONArray data = new JSONArray();
data.put(userdata);
System.out.println("Array is: " + data);
try {
DataOutputStream localDataOutputStream = new DataOutputStream(httpConn.getOutputStream());
localDataOutputStream.writeBytes(data.toString());
localDataOutputStream.flush();
localDataOutputStream.close();
System.out.println("Data writting: " + data);
} catch (IOException e) {
e.printStackTrace();
System.out.println("Data Output error");
}
int responseCode = httpConn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
do {
line = br.readLine();
response += line;
} while ((line = br.readLine()) != null);
} else {
response = "Error ";
throw new IOException();
}
return response + " * Uploaded!";
}
}
}
我使用了JSONObject并将用户名和密码的键/值对放入其中。然后我把这个JSONObject放在JSONArray中(我知道它只是一个JSONObject,我不必把它放在JSONArray中,但由于我的PHP代码是为JSONArray“设计”的,我不擅长PHP,我认为把JSONObject放在JSONArray中比修改我不太理解的PHP代码更容易。
第二个变化是使用“DataOutputStream”而不是“OutputStreamWriter”。