我正在开发一个应用程序,这里我想使用共享首选项来维护会话。在接下来的页面我给出了会话但是现在我想在php文件中使用这个会话电子邮件来获取当前用户的数据,怎么做我把这个会话的电子邮件发送到php文件?请建议如何解决这个问题。
//java file
public class DetailsActivity2 extends Activity {
TextView uid;
TextView name1, address1, seats1, email1;
TextView amount1;
Button Btngetdata;
private boolean loggedIn = false;
//URL to get JSON Array
private static String url = "http://example.in/ticket1.php";
//JSON Node Names
private static final String TAG_USER = "result";
private static final String TAG_NAME = "pname";
private static final String TAG_AMOUNT = "pamount";
private static final String TAG_ADDRESS = "paddress";
private static final String TAG_SEATS = "pseats";
JSONArray result = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details_activity);
Btngetdata = (Button)findViewById(R.id.button3_submit);
email1=(TextView)findViewById(R.id.textView_email);
new JSONParse().execute();
Btngetdata.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sharedPreferences = getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);
loggedIn = sharedPreferences.getBoolean(Config.LOGGEDIN_SHARED_PREF, false);
String email = sharedPreferences.getString(Config.EMAIL_SHARED_PREF, "Not Available");
email1.setText(email);
if(loggedIn){
Intent intent = new Intent(DetailsActivity2.this, LoginActivity.class);
startActivity(intent);
}
}
});
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
// uid = (TextView)findViewById(R.id.uid);
name1 = (TextView)findViewById(R.id.textView_name1);
amount1 = (TextView)findViewById(R.id.textView_amount1);
address1= (TextView)findViewById(R.id.textView_address1);
seats1= (TextView)findViewById(R.id.textView_sname1);
pDialog = new ProgressDialog(DetailsActivity2.this);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {
// Getting JSON Array
result = json.getJSONArray(TAG_USER);
JSONObject c = result.getJSONObject(0);
// Storing JSON item in a Variable
// String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String amount = c.getString(TAG_AMOUNT);
String seats = c.getString(TAG_SEATS);
String address = c.getString(TAG_ADDRESS);
//Set JSON Data in TextView
// uid.setText(id);
name1.setText(name);
address1.setText(address);
seats1.setText(seats);
amount1.setText(amount);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
//php file
<?php
$id= $_GET['email'];
$sql = "SELECT * FROM tbl_users WHERE email='$id'";
$con=mysqli_connect("localhost","user","pass","db");
$r =mysqli_query($con,$sql);
$result =array();
while($row =mysqli_fetch_array($r)){
array_push($result,array(
'pname'=>$row['pname'],
'paddress'=>$row['paddress'],
'pseats'=>$row['pseats'],
'pamount'=>$row['pamount']
));
}
echo json_encode(array('result'=>$result));
mysqli_close($con);
?>
答案 0 :(得分:1)
public class FileUploader {
private static final String LINE_FEED = "\r\n";
private final String boundary;
private HttpsURLConnection httpConn;
private String charset;
private OutputStream outputStream;
private PrintWriter writer;
public FileUploader(String requestURL, String charset)
throws IOException {
this.charset = charset;
// creates a unique boundary based on time stamp
boundary = "===" + System.currentTimeMillis() + "===";
URL url = new URL(requestURL);
httpConn = (HttpsURLConnection) url.openConnection();
httpConn.setUseCaches(false);
httpConn.setDoOutput(true); // indicates POST method
httpConn.setDoInput(true);
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);
httpConn.setRequestProperty("User-Agent", "CodeJava Agent");
outputStream = httpConn.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(outputStream, charset),
true);
}
/**
* Adds a form field to the request
*
* @param name field name
* @param value field value
*/
public void addFormField(String name, String value) {
writer.append("--").append(boundary).append(LINE_FEED);
writer.append("Content-Disposition: form-data; name=\"").append(name).append("\"")
.append(LINE_FEED);
writer.append("Content-Type: text/plain; charset=").append(charset).append(
LINE_FEED);
writer.append(LINE_FEED);
writer.append(value).append(LINE_FEED);
writer.flush();
}
/**
* Adds a upload file section to the request
*
* @param fieldName name attribute in <input type="file" name="..." />
* @param uploadFile a File to be uploaded
* @throws IOException
*/
public void addFilePart(String fieldName, File uploadFile)
throws IOException {
String fileName = uploadFile.getName();
writer.append("--").append(boundary).append(LINE_FEED);
writer.append("Content-Disposition: form-data; name=\"").append(fieldName).append("\"; filename=\"").append(fileName).append("\"")
.append(LINE_FEED);
writer.append("Content-Type: ").append(URLConnection.guessContentTypeFromName(fileName))
.append(LINE_FEED);
writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
writer.append(LINE_FEED);
writer.flush();
FileInputStream inputStream = new FileInputStream(uploadFile);
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
inputStream.close();
writer.append(LINE_FEED);
writer.flush();
}
/**
* Adds a header field to the request.
*
* @param name - name of the header field
* @param value - value of the header field
*/
public void addHeaderField(String name, String value) {
writer.append(name).append(": ").append(value).append(LINE_FEED);
writer.flush();
}
/**
* Completes the request and receives response from the server.
*
* @return a list of Strings as response in case the server returned
* status OK, otherwise an exception is thrown.
* @throws IOException
*/
public String finish() {
String response = "";
writer.append(LINE_FEED).flush();
writer.append("--").append(boundary).append("--").append(LINE_FEED);
writer.close();
// checks server's status code first
int status = 0;
try {
status = httpConn.getResponseCode();
if (status == HttpsURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(
httpConn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
response += line;
}
reader.close();
httpConn.disconnect();
} else {
return null;
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
return response;
}
}
然后
FileUploader fileUploader = new FileUploader("http://example.in/ticket1.php","UTF-8");
fileUploader.addFormField("email","anonymous@gmail.com");
String response = fileUploader.finish();