Java 如何在Android中与jsonwebservices集成?
我正在Android应用程序中实现json_web服务。我想在用Java创建的jsonwebservices上发送json数据。当我运行应用程序时,数据不会从Android发送,不会显示任何错误,也不会显示任何类型的异常 我如何确定我的数据是否已发送 这是我的活动代码:Java 如何在Android中与jsonwebservices集成?,java,android,json,Java,Android,Json,我正在Android应用程序中实现json_web服务。我想在用Java创建的jsonwebservices上发送json数据。当我运行应用程序时,数据不会从Android发送,不会显示任何错误,也不会显示任何类型的异常 我如何确定我的数据是否已发送 这是我的活动代码: public class Login extends Activity { Button btnLogin; EditText etextUsername , etextPassword; Strin
public class Login extends Activity
{
Button btnLogin;
EditText etextUsername , etextPassword;
String strUserName , strPassWord ;
ProgressDialog pDialog;
JSONObject jObject ;
SharedPreferences.Editor editor;
SharedPreferences sharedPref1;
String str_Device_IP_Address=null;
JSONArray user = null;
String pref_filename = "IP_ADDRESS";
static final String KEY_REQUEST_ID = "RequestId";
static final String KEY_REQUEST_CODE = "RequestCode";
static final String KEY_CHANNEL_ID = "ChannelId";
static final String KEY_IP_ADDRESS="IPAddress";
static final String KEY_USERNAME="UserId";
static final String KEY_PASSWORD="Password";
static final String KEY_REQUEST="Request";
static final String KEY_VENDOR_ID="VendorId";
String RequestId="77777";
String RequestCode="001";
String stringChannelId="MobileApp";
String strIpAddress = null;
private String textToEncrypt = "Hi, this is a test to check its gone work or not.";
String encrypted = "MzA3RDBCMjMxMjQzNzcxREUxMUYxNjg1NzgwOTU1MjU1M0FDOUZEN0M3Q0JGQ0Q5MTI2NEIyNTE2"
+ "OTQwQTc3NjM2QTBCRDFDMUEyNkUwRjlDMzQwN0U0MEI0NDg2M0JBMDU1OThCNTI1NTZCMEFGNjk1NjJFNzZBMUE0NzM4NTQ=";
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
final Context context = getApplicationContext();
connectWithHttpGet_IpAddress();
etextUsername = (EditText)findViewById(R.id.edittext_username);
etextPassword = (EditText)findViewById(R.id.edittext_password);
btnLogin=(Button)findViewById(R.id.button_Login);
btnLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
if (!isOnline())
{
showNoConnectionDialog(Login.this);
}
else
{
connectWithHttpGet_LoginData();
}
}
});
}
private void connectWithHttpGet_LoginData()
{
class GetJSONParse extends AsyncTask<String, Integer, JSONObject>
{
@Override
protected void onPreExecute()
{
super.onPreExecute();
str_Device_IP_Address=sharedPref1.getString("ip_address", "a\n");
System.out.println("strCode in Gsk_Demo ="+str_Device_IP_Address);
strUserName = etextUsername.getText().toString().trim();
strPassWord = etextPassword.getText().toString().trim();
pDialog = new ProgressDialog(Login.this);
pDialog.setIndeterminate(true);
pDialog.setCancelable(true);
pDialog.show();
System.out.println("Progress Dialog!!!!!!!!!!!!!!!!!!!!!!!");
}
@Override
protected JSONObject doInBackground(String... args)
{
String strUrl = "http://test.xxxxxx.com/cms/json/w2iWS";
JSONParser jParser = new JSONParser();
Log.e("DoinBackground !!!!!","Method");
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(strUrl);
String jsonString=json.toString();
Log.e("jsonString in DoinBackground !!!!!","Method" + jsonString);
return json;
}
@Override
protected void onPostExecute(JSONObject json)
{
pDialog.dismiss();
try
{
// Getting JSON Array
user = json.getJSONArray( KEY_REQUEST_ID );
JSONObject jsonObject = user.getJSONObject(0);
jsonObject.put(KEY_REQUEST_CODE, RequestCode);
jsonObject.put(KEY_CHANNEL_ID, stringChannelId);
jsonObject.put(KEY_IP_ADDRESS, str_Device_IP_Address);
jsonObject.put(KEY_USERNAME, strUserName);
jsonObject.put(KEY_PASSWORD, strPassWord);
String encrypted1 = EncodeDecodeAES.encrypt(jsonObject.toString(), textToEncrypt);
System.out.println("encrypted1 =" + encrypted1);
JSONObject inner = new JSONObject();
inner.put(KEY_REQUEST, encrypted1);
inner.put(KEY_VENDOR_ID, "1");
String decrypted = EncodeDecodeAES.decrypt(jsonObject.toString(), encrypted);
System.out.println("decrypted =" + decrypted);
}
catch (JSONException e)
{
e.printStackTrace();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
GetJSONParse getjsonparse = new GetJSONParse();
getjsonparse.execute();
}
// Get Ip Address
private void connectWithHttpGet_IpAddress() {
class httpGetAsynchTask extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
HttpClient httpClient = new DefaultHttpClient();
String url = "http://api.externalip.net/ip";
Log.e("!!STRING URL DATE DETAIL", "" + url);
HttpGet httpGet = new HttpGet(url);
Log.e("", "" + httpGet);
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
Log.e("HTTP.RESPONSE.DATE.DTAIL", "" + httpResponse);
System.out.println("HTTPRESPONSE");
InputStream inpustream = httpResponse.getEntity()
.getContent();
InputStreamReader inputstreamreader = new InputStreamReader(
inpustream);
BufferedReader bufferedreader = new BufferedReader(
inputstreamreader);
StringBuilder stringbuilder = new StringBuilder();
Log.e("", "" + stringbuilder);
String strbuffer = null;
while ((strbuffer = bufferedreader.readLine()) != null)
{
stringbuilder.append(strbuffer);
}
String strResponse = stringbuilder.toString();
/****************** Code For Shared Preferences **************************************/
sharedPref1 = getSharedPreferences(pref_filename, 0);
editor = sharedPref1.edit();
editor.putString("ip_address", strResponse);
Log.e("Returning value of doInBackground REsponse:" ,strResponse);
System.out.println("IPADDRESS IN DOIN BACKGRAOUND");
editor.commit();
/***************** Code For Shared Preferences **************************************/
}
catch (ClientProtocolException cpe) {
cpe.printStackTrace();
Log.e("Exception generates caz of httpResponse :", "-"
+ cpe);
}
catch (IOException ioe) {
ioe.printStackTrace();
Log.e("Second exception generates caz of httpResponse :",
"-" + ioe);
}
return null;
}
}
httpGetAsynchTask httpGetAsyncTask = new httpGetAsynchTask();
httpGetAsyncTask.execute();
}
public static void showNoConnectionDialog(final Login login)
{
AlertDialog.Builder builder = new AlertDialog.Builder(login);
builder.setCancelable(true);
builder.setMessage(R.string.no_connection);
builder.setTitle(R.string.no_connection_title);
builder.setPositiveButton(R.string.settings_button_text, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
login.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
}
});
builder.setNegativeButton(R.string.cancel_button_text, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
return;
}
});
builder.setOnCancelListener(new DialogInterface.OnCancelListener()
{
@Override
public void onCancel(DialogInterface dialog) {
return;
}
});
builder.show();
}
public boolean isOnline()
{
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()
&& cm.getActiveNetworkInfo().isAvailable()
&& cm.getActiveNetworkInfo().isConnected())
{
return true;
}
else
{
return false;
}
}
}
公共类登录扩展活动
{
按钮btnLogin;
EditText-etextUsername、etextPassword;
字符串strUserName,strPassWord;
ProgressDialog;
JSONObject jObject;
SharedReferences.Editor;
SharedReferences SharedRefer1;
字符串str\u Device\u IP\u Address=null;
JSONArray user=null;
字符串pref_filename=“IP_地址”;
静态最终字符串键\u请求\u ID=“RequestId”;
静态最终字符串键\u REQUEST\u CODE=“RequestCode”;
静态最终字符串键\u CHANNEL\u ID=“ChannelId”;
静态最终字符串KEY\u IP\u ADDRESS=“IPAddress”;
静态最终字符串键\u USERNAME=“UserId”;
静态最终字符串键\u PASSWORD=“PASSWORD”;
静态最终字符串键\u REQUEST=“REQUEST”;
静态最终字符串键\u VENDOR\u ID=“VendorId”;
字符串RequestId=“77777”;
字符串RequestCode=“001”;
字符串stringChannelId=“MobileApp”;
字符串strIpAddress=null;
private String textToEncrypt=“您好,这是一个检查它是否工作正常的测试。”;
字符串加密=“mza3rdbcmjmxmjqznzcxreuxmuyxnjg1nzgwotu1mju1m0fdouzen0m3q0jgq0q5mti2neyinte2”
+“OTQWQTC3NJM2QTBCRDFDMUEYNKUWRJLDMZQWN0U0MEI0NDG2M0JBMDU1THCNTI1NTZCMEFGNJK1NJJFNZBMUE0NZM4NTQ=”;
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
最终上下文=getApplicationContext();
连接httpget_IpAddress();
etextUsername=(EditText)findViewById(R.id.EditText\u用户名);
etextPassword=(EditText)findViewById(R.id.EditText\u密码);
btnLogin=(按钮)findViewById(R.id.Button\u登录);
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图arg0)
{
//TODO自动生成的方法存根
如果(!isOnline())
{
showNoConnectionDialog(Login.this);
}
其他的
{
连接httpget_loginda();
}
}
});
}
私有void connectWithHttpGet_loginda()
{
类GetJSONParse扩展了AsyncTask
{
@凌驾
受保护的void onPreExecute()
{
super.onPreExecute();
str_Device_IP_Address=sharedPref1.getString(“IP_Address”,“a\n”);
System.out.println(“Gsk_演示中的strCode=“+str_设备_IP_地址”);
strUserName=etextUsername.getText().toString().trim();
strPassWord=etextPassword.getText().toString().trim();
pDialog=newprogressdialog(Login.this);
pDialog.setUndeterminate(真);
pDialog.setCancelable(真);
pDialog.show();
System.out.println(“进度对话框!!!!!!!!!!!!!!!”;
}
@凌驾
受保护的JSONObject doInBackground(字符串…args)
{
字符串strUrl=”http://test.xxxxxx.com/cms/json/w2iWS";
JSONParser jParser=新的JSONParser();
Log.e(“DoinBackground!!!”,“Method”);
//从URL获取JSON
JSONObject json=jParser.getJSONFromUrl(strUrl);
字符串jsonString=json.toString();
Log.e(“DoinBackground中的jsonString!!!”,“Method”+jsonString);
返回json;
}
@凌驾
受保护的void onPostExecute(JSONObject json)
{
pDialog.disclose();
尝试
{
//获取JSON数组
user=json.getJSONArray(KEY\u REQUEST\u ID);
JSONObject JSONObject=user.getJSONObject(0);
jsonObject.put(键请求代码,请求代码);
jsonObject.put(KEY\u CHANNEL\u ID,stringChannelId);
jsonObject.put(密钥IP地址、str设备IP地址);
jsonObject.put(KEY_用户名,strUserName);
jsonObject.put(关键字密码,strPassWord);
String encrypted1=encodecodeAES.encrypt(jsonObject.toString(),textToEncrypt);
System.out.println(“encrypted1=“+encrypted1”);
JSONObject inner=新的JSONObject();
内部.put(密钥请求,加密1);
内部。put(关键供应商ID,“1”);
String decrypted=encodecodeaes.decrypt(jsonObject.toString(),加密);
System.out.println(“decrypted=“+decrypted”);
}
捕获(JSONException e)
{
e、 printStackTrace();
}
捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
GetJSONParse GetJSONParse=新建GetJSONParse();
getjsonparse.execute();
}
//获取Ip地址
私有void connectWithHttpGet_IpAddress(){
类httpGetAsynchTask扩展了AsyncTask{
JSONParse jp =new JSONParse();
jp.execute(params);
GetJSONParse get = new GetJSONParse();
get.execute(params);