我正在尝试通过发送请求进行加密和解密,并从我的android应用程序中的服务器获取响应,并且我使用了JSON网络服务,但是却遇到了RuntimeException。我不知道该怎么做。
这是我的活动代码。
public class MainActivity extends Activity implements OnClickListener {
Button btnPost;
static String key="Dyv6ACIDe2q+OEjztjfNDw==";
Person person;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPost = (Button) findViewById(R.id.btnPost);
btnPost.setOnClickListener(this);
}
static String RequestId="3465768789";
static String strRequestCode="001";
static String stringChannelId="MobileApp";
static String strIpAddress = "192.168.1.123";
static String strStatusFlag="true";
static String strUserId="[email protected]";
static String strPassword="password1";
static String strVendorId="001";
static String strRequestId="09";
static String strRequest="CLIENT";
public static String POST(String url, Person person)
{
InputStream inputStream = null;
String result = "";
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
String json = "";
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("RequestId", strRequestId);
jsonObject.accumulate("RequestCode", strRequestCode);
jsonObject.accumulate("ChannelId", stringChannelId);
jsonObject.accumulate("IPAddress", strIpAddress);
jsonObject.accumulate("ForceChangeFlag",strStatusFlag);
jsonObject.accumulate("UserId", strUserId);
jsonObject.accumulate("Password",strPassword);
jsonObject.accumulate("Request",strRequest );
jsonObject.accumulate("VendorId", strVendorId);
json = jsonObject.toString();
String reqvalues=AES128Bit.encrypt(jsonObject.toString().toString(),key);
Log.e("Reqvalues"," = "+reqvalues);
StringEntity se = new StringEntity(reqvalues);
httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
HttpResponse httpResponse = httpclient.execute(httpPost);
inputStream = httpResponse.getEntity().getContent();
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
}
catch (Exception e)
{
Log.d("InputStream", e.getLocalizedMessage());
}
return result;
}
@Override
public void onClick(View view)
{
new HttpAsyncTask().execute("http://test.window2india.com/cms/json/w2iWS");
}
}
private class HttpAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls)
{
person = new Person();
person.setRequestId(strRequestId);
person.setRequestCode(strRequestCode);
person.setChannelId(stringChannelId);
person.setIPAddress(strIpAddress);
person.setStatusFlag(strStatusFlag);
person.setUserId(strUserId);
person.setPassword(strPassword);
person.setRequest(strRequest);
person.setVendorId(strVendorId);
String strPerson=person.toString();
Log.e("strPerson ", " =" + strPerson);
return POST(urls[0],person);
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
Toast.makeText(getBaseContext(), "Data Sent!", Toast.LENGTH_LONG).show();
}
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException{
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
}
这是日志猫信息。
12-28 10:21:15.183: I/dalvikvm(417): Could not find method com.anm.cms.core.util.AES128Bit.encrypt, referenced from method com.json_to_server.MainActivity.POST
12-28 10:21:15.223: W/dalvikvm(417): VFY: unable to resolve static method 5048: Lcom/anm/cms/core/util/AES128Bit;.encrypt (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
12-28 10:21:15.223: D/dalvikvm(417): VFY: replacing opcode 0x71 at 0x0061
12-28 10:21:15.362: D/dalvikvm(417): VFY: dead code 0x0064-00a0 in Lcom/json_to_server/MainActivity;.POST (Ljava/lang/String;Lcom/json_to_server/Person;)Ljava/lang/String;
12-28 10:21:15.362: D/dalvikvm(417): VFY: dead code 0x00a2-00a4 in Lcom/json_to_server/MainActivity;.POST (Ljava/lang/String;Lcom/json_to_server/Person;)Ljava/lang/String;
12-28 10:21:33.963: E/strPerson(417): =com.json_to_server.Person@40527d08
12-28 10:21:33.993: W/dalvikvm(417): threadid=9: thread exiting with uncaught exception (group=0x40015560)
12-28 10:21:34.003: E/AndroidRuntime(417): FATAL EXCEPTION: AsyncTask #1
12-28 10:21:34.003: E/AndroidRuntime(417): java.lang.RuntimeException: An error occured while executing doInBackground()
12-28 10:21:34.003: E/AndroidRuntime(417): at android.os.AsyncTask$3.done(AsyncTask.java:200)
12-28 10:21:34.003: E/AndroidRuntime(417): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
12-28 10:21:34.003: E/AndroidRuntime(417): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
12-28 10:21:34.003: E/AndroidRuntime(417): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
12-28 10:21:34.003: E/AndroidRuntime(417): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
12-28 10:21:34.003: E/AndroidRuntime(417): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
12-28 10:21:34.003: E/AndroidRuntime(417): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
12-28 10:21:34.003: E/AndroidRuntime(417): at java.lang.Thread.run(Thread.java:1019)
12-28 10:21:34.003: E/AndroidRuntime(417): Caused by: java.lang.NoClassDefFoundError: com.anm.cms.core.util.AES128Bit
12-28 10:21:34.003: E/AndroidRuntime(417): at com.json_to_server.MainActivity.POST(MainActivity.java:120)
12-28 10:21:34.003: E/AndroidRuntime(417): at com.json_to_server.MainActivity$HttpAsyncTask.doInBackground(MainActivity.java:202)
12-28 10:21:34.003: E/AndroidRuntime(417): at com.json_to_server.MainActivity$HttpAsyncTask.doInBackground(MainActivity.java:1)
12-28 10:21:34.003: E/AndroidRuntime(417): at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-28 10:21:34.003: E/AndroidRuntime(417): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
12-28 10:21:34.003: E/AndroidRuntime(417): ... 4 more
最佳答案
您的logcat指出您尚未在类路径中保留所需的库(可能是构建路径可以引用该库,因此不会出现编译错误)。
12-28 10:21:34.003: E/AndroidRuntime(417): Caused by: java.lang.NoClassDefFoundError: com.anm.cms.core.util.AES128Bit
请确保您也在类路径中引用了正确的库。
编辑:您也可以尝试通过以下过程确保jar在运行时可用;
转到Java Build Path and Order and Export选项卡
确保您的外部jar已被“检查”
将其移到列表顶部