我有这个错误
错误:(27,57)错误:未报告的异常异常;必须抓住 或宣布被抛出
在这一行:
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;
}
}
任何人都可以帮忙吗?我很感激。
答案 0 :(得分:1)
将以下构造函数添加到您的类
MCrypt mcrypt;
public String encrypted;
public MainActivity(){
try{
mcrypt = new MCrypt();
encrypted = MCrypt.bytesToHex( mcrypt.encrypt("Text to Encrypt") );
} catch(Exception e){
e.printStackTrace();
}
}
通过替换两条相应的行。
答案 1 :(得分:0)
如果你想用try-catch包围它,代码需要在构造函数中。或者这样做:
public class MainActivity extends Activity {
public String encrypted = bytesToHex();
private static String bytesToHex() {
try {
MCrypt mcrypt = new MCrypt();
return MCrypt.bytesToHex(mcrypt.encrypt("Text to Encrypt"));
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}