public String encrypted = MCrypt.bytesToHex(mcrypt.encrypt("Text to Encrypt"));
とtryおよびcatchとそれを取り巻くしようとしたが、それはうまくいきませんでした。
package com.example.user.test14;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
MCrypt mcrypt = new MCrypt();
public String encrypted = MCrypt.bytesToHex(mcrypt.encrypt("Text to Encrypt"));
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new MyTask().execute();
}
private class MyTask extends AsyncTask<String, String, Boolean> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Boolean doInBackground(String... params) {
List<NameValuePair> params1 = new ArrayList<NameValuePair>();
params1.add(new BasicNameValuePair("id","1"));
params1.add(new BasicNameValuePair("temp",encrypted));
params1.add(new BasicNameValuePair("pulse","sheri"));
params1.add(new BasicNameValuePair("motion","a"));
String link="http://192.168.0.21/insert_data.php";
boolean success = false;
try {
JSONObject json = makeHttpRequest(link, "POST", params1);
success = json.getBoolean("success");
} catch (Exception e) {
// TODO: handle exception
}
return success;
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if (result) {
Log.e("result","true");
} else {
Log.e("result","false");
}
}
}
private JSONObject makeHttpRequest(String url, String method,List<NameValuePair> params) {
InputStream is = null;
JSONObject jObj = null;
String json = "";
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, HTTP.UTF_8), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
Log.e("json", json);
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
誰でも助けてください。それは有り難いです。
コンストラクタに移動してみましたか? – Pavan
@Pavan私はどのように、私はまだAndroidの新しいですか分からない。 –