Java 在真实设备上调试Android应用程序与Servlet通信
所以这个场景是我试图在我的真实设备上调试一个简单的应用程序,我使用eclipse来制作这个应用程序 现在,我的应用程序正在与服务器上的servlet通信,并且应用程序在genymotion emulator中运行得非常顺利(不使用eclipse提供的仿真器)。但当我尝试在我的设备上运行它时,它在一个活动类之前一直工作正常,但在第二个活动中应用程序崩溃。 所以,经过这么多的搜索,我把这个问题贴在这里。希望我能找到解决办法 这是我的第二项活动:Java 在真实设备上调试Android应用程序与Servlet通信,java,android,servlets,android-asynctask,Java,Android,Servlets,Android Asynctask,所以这个场景是我试图在我的真实设备上调试一个简单的应用程序,我使用eclipse来制作这个应用程序 现在,我的应用程序正在与服务器上的servlet通信,并且应用程序在genymotion emulator中运行得非常顺利(不使用eclipse提供的仿真器)。但当我尝试在我的设备上运行它时,它在一个活动类之前一直工作正常,但在第二个活动中应用程序崩溃。 所以,经过这么多的搜索,我把这个问题贴在这里。希望我能找到解决办法 这是我的第二项活动: public void onCreate(Bundle
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.success);
Intent objIntent = getIntent();
s2 = objIntent.getStringExtra("repname");
findViewsById();
submit.setOnClickListener(this);
}
private void findViewsById() {
submit = (Button) findViewById(R.id.submit);
headerText = (TextView) findViewById(R.id.tv1);
headerText.setVisibility(View.INVISIBLE);
submit2 = (Button) findViewById(R.id.secondSubmit);
submit2.setVisibility(View.INVISIBLE);
t1= (TextView) findViewById(R.id.tvexample1);
t2= (TextView) findViewById(R.id.tvexample2);
submit2.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
//DO SOMETHING! {RUN SOME FUNCTION ... DO CHECKS... ETC}
lviewAdapter.clear();
lviewAdapter.notifyDataSetChanged();
Calendar cal = Calendar.getInstance();
System.out.println("what is from calendar"+cal);
Date currentLocalTime = cal.getTime();
//System.out.println("what is from CurrentLocalTime"+currentLocalTime);
DateFormat date = new SimpleDateFormat("yyyy-MM-dd");
date.setTimeZone(TimeZone.getTimeZone("GMT"));
//System.out.println("what is from date"+date);
String localTime = date.format(currentLocalTime);
//localTime = "2014-08-26";
System.out.println("and result is == " + localTime);
pb.setVisibility(View.VISIBLE);
new MyAsyncTask().execute(localTime,s2);
}
});
pb=(ProgressBar)findViewById(R.id.progressBar1);
pb.setVisibility(View.GONE);
c=this;
}
public void onClick(View view) {
Log.d("1:", "in the onclick");
Calendar cal = Calendar.getInstance();
System.out.println("what is from calendar"+cal);
Date currentLocalTime = cal.getTime();
//System.out.println("what is from CurrentLocalTime"+currentLocalTime);
DateFormat date = new SimpleDateFormat("yyyy-MM-dd");
date.setTimeZone(TimeZone.getTimeZone("GMT"));
//System.out.println("what is from date"+date);
String localTime = date.format(currentLocalTime);
//localTime = "2014-08-26";
System.out.println("and result is == " + localTime);
pb.setVisibility(View.VISIBLE);
new MyAsyncTask().execute(localTime,s2);
}
// @Override
// public void onBackPressed() {
// Log.d("CDA", "onBackPressed Called");
// Intent setIntent = new Intent(Intent.ACTION_MAIN);
// setIntent.addCategory(Intent.CATEGORY_HOME);
// setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// startActivity(setIntent);
// }
private class MyAsyncTask extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
Log.d("tag1","in do in ");
String s=postData(params);
Log.d("tag2","in do in SSS ");
//Printing this 5 th
Log.d("what is s",s);
return s;
}
protected void onPostExecute(String result){
Log.d("on post ","on post execute");
pb.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(),"Appointment Displayed", Toast.LENGTH_SHORT).show();
//Log.d("tag",result);
init(result);
submit.setVisibility(View.GONE);
headerText.setVisibility(View.VISIBLE);
submit2.setVisibility(View.VISIBLE);
}
}
protected void onProgressUpdate(Integer... progress){
pb.setProgress(progress[0]);
}
public String postData(String valueIWantToSend[]) {
String origresponseText="";
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("CurrentDate",valueIWantToSend[0]));
nameValuePairs.add(new BasicNameValuePair("repname", valueIWantToSend[1]));
System.out.println("CurrentDate"+valueIWantToSend[0]);
System.out.println("username"+valueIWantToSend[1]);
exampleString1= valueIWantToSend[0];
exampleString2= valueIWantToSend[1];
HttpClient httpclient = new DefaultHttpClient();
HttpParams params = httpclient.getParams();
HttpConnectionParams.setConnectionTimeout(httpclient.getParams(),10000000);
//httppost = new HttpPost("http://192.168.56.1:8080/First/Hello");
httppost = new HttpPost("http://203.199.134.131:8080/First/Hello");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
/* execute */
HttpResponse response = httpclient.execute(httppost);
System.out.println("response from servlet"+response.toString());
origresponseText=readContent(response);
Log.d("response", origresponseText);
}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
}
catch (IOException e) {
// TODO Auto-generated catch block
}
//removing unwated "" and other special symbols from response
String responseText = origresponseText.substring(1,origresponseText.length() -2 );
Log.d("Response tag", responseText);
return responseText;
}
// }
String readContent(HttpResponse response)
{
String text = "";
InputStream in =null;
try {
in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
//Log.d("", line);
sb.append(line + "\n");
//Printing this first
// Log.d("", line);
// Log.e("TAGpppp", ">>>>>PRINTING<<<<<");
// Log.e("TAGiiii", in.toString());
}
text = sb.toString();
Log.d("TEXT", text);
}
catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
in.close();
} catch (Exception ex) {
}
}
return text;
}
@SuppressWarnings("deprecation")
public void init(String result) {
System.out.println(result);
t1.setText(exampleString1);
t2.setText(exampleString2);
String response= result + "}";
System.out.println(response);
try {
JSONObject jsonArray = new JSONObject(response);
System.out.println("1:"+jsonArray);
//ArrayList obj1 = new ArrayList();
JSONArray obj1 = jsonArray.getJSONArray("get");
System.out.println("1:"+obj1);
for (int i = 0; i < obj1.length(); i++) {
System.out.println("Length of array"+obj1.length());
JSONObject results = obj1.getJSONObject(i);
System.out.println("2:"+results);
String pcode= results.getString("ProspCustCode");
System.out.println("Prospect code"+pcode);
String date= results.getString("FollowUpDate");
System.out.println("FollowUpDate"+date);
String time= results.getString("FollowUpTime");
System.out.println("FollowUpTime"+time);
String status= results.getString("Status");
System.out.println("Status"+status);
String ftype= results.getString("FollowUpType");
System.out.println("FollowUpType"+ftype);
String ntime= results.getString("NextFollowUpTime");
System.out.println("NextFollowUpTime"+ntime);
String cname= results.getString("ContactPerson");
System.out.println("ContactPerson"+cname);
String desig= results.getString("Designation");
System.out.println("Designation"+desig);
String com= results.getString("Comments");
System.out.println("Comments"+com);
String spoke= results.getString("SpokenTo");
System.out.println("SpokenTo"+spoke);
insertdata(pcode,date,time,status,ftype,ntime,cname,desig,com,spoke);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void insertdata(String PROSPCUSTCODE, String DATE, String TIME,
String STATUS, String FOLLOWUPTYPE, String NEXTFOLLOWUPTIME, String NAME,
String DESIGNATION, String COMMENTS, String SPOKENTO) {
HashMap<String, String> queryValues = new HashMap<String, String>();
queryValues.put("ProspCustCode", PROSPCUSTCODE);
queryValues.put("Date", DATE);
queryValues.put("Time", TIME);
queryValues.put("Status", STATUS);
queryValues.put("FollowUpType", FOLLOWUPTYPE);
queryValues.put("NextFollowUpTime", NEXTFOLLOWUPTIME);
queryValues.put("Name", NAME);
queryValues.put("Designation", DESIGNATION);
queryValues.put("Comments", COMMENTS);
queryValues.put("SpokenTo", SPOKENTO);
controller.insertDeails(queryValues);
//this.callHomeActivity(view);
DBController dbHelper = new DBController(this.getApplicationContext());
newDB = dbHelper.getWritableDatabase();
Cursor cursor = newDB.rawQuery("SELECT * FROM TempFollowUpDetails", null);
if (cursor != null ) {
if (cursor.moveToFirst()) {
do {
System.out.println(cursor.getColumnIndex("ProspCustCode"));
//String ProspCustCode = c.getString(c.getColumnIndex("ProsCustCode"));
String Date = cursor.getString(cursor.getColumnIndex("Date"));
String Time = cursor.getString(cursor.getColumnIndex("Time"));
String Status = cursor.getString(cursor.getColumnIndex("Status"));
String NextFollowUpTime = cursor.getString(cursor.getColumnIndex("NextFollowUpTime"));
Name = cursor.getString(cursor.getColumnIndex("Name"));
System.out.println(cursor.getString(cursor.getColumnIndex("Name")));
String Designation = cursor.getString(cursor.getColumnIndex("Designation"));
String Comments = cursor.getString(cursor.getColumnIndex("Comments"));
String SpokenTo = cursor.getString(cursor.getColumnIndex("SpokenTo"));
String Id = cursor.getString(cursor.getColumnIndex("Id"));
//ProspCustCodeArray.add(ProspCustCode);
DateArray.add(Date);
TimeArray.add(Time);
StatusArray.add(Status);
NextFollowUpTimeArray.add(NextFollowUpTime);
NameArray.add(Name);
DesignationArray.add(Designation);
CommentsArray.add(Comments);
SpokenToArray.add(SpokenTo);
IdArray.add(Id);
}while (cursor.moveToNext());
}
displaylist(IdArray,NameArray);
}
System.out.println("Elements of name array"+NameArray);
System.out.println("Elements of name array"+DateArray);
System.out.println("Elements of name array"+TimeArray);
System.out.println("Elements of name array"+DesignationArray);
System.out.println("Elements of name array"+CommentsArray);
System.out.println("Elements of name array"+IdArray);
//............. For normal listview
}
private void displaylist(ArrayList<String> idArray2,
ArrayList<String> nameArray2) {
// TODO Auto-generated method stub
listView = (ListView) findViewById(R.id.listViewAnimals);
lviewAdapter = new ListCustomAdapter(this, idArray2, nameArray2);
System.out.println("adapter => "+lviewAdapter.getCount());
listView.setAdapter(this.lviewAdapter);
controller.deleteDetails(null);
//... start appointments details activity to show the details on item click listener
this.listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View viewClicked, int position, long id) {
TextView tv1 =(TextView)viewClicked.findViewById(R.id.lblListItem);
Intent intent = new Intent(Success.this, AppointmentDetails.class);
intent.putExtra("name", tv1.getText().toString());
startActivity(intent);
}
}); }
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.success);
Intent objIntent=getIntent();
s2=objIntent.getStringExtra(“repname”);
findViewsById();
submit.setOnClickListener(这个);
}
私有void findViewsById(){
提交=(按钮)findViewById(R.id.submit);
headerText=(TextView)findViewById(R.id.tv1);
headerText.setVisibility(视图不可见);
submit2=(按钮)findViewById(R.id.secondSubmit);
submit2.setVisibility(视图.不可见);
t1=(TextView)findViewById(R.id.tvexample1);
t2=(TextView)findViewById(R.id.tvexample2);
submit2.setOnClickListener(新的OnClickListener(){
公共void onClick(视图v)
{
//做点什么!{运行一些函数…做检查…等等}
lviewAdapter.clear();
lviewAdapter.notifyDataSetChanged();
Calendar cal=Calendar.getInstance();
System.out.println(“来自日历的内容”+cal);
Date currentLocalTime=cal.getTime();
//System.out.println(“来自CurrentLocalTime的内容”+CurrentLocalTime);
日期格式日期=新的简化格式(“yyyy-MM-dd”);
date.setTimeZone(TimeZone.getTimeZone(“GMT”));
//System.out.println(“起始日期”+日期);
字符串localTime=date.format(currentLocalTime);
//localTime=“2014-08-26”;
System.out.println(“结果为==”+localTime);
pb.setVisibility(View.VISIBLE);
新建MyAsyncTask().execute(localTime,s2);
}
});
pb=(ProgressBar)findViewById(R.id.progressBar1);
pb.setVisibility(View.GONE);
c=这个;
}
公共void onClick(视图){
Log.d(“onclick中的1:,”);
Calendar cal=Calendar.getInstance();
System.out.println(“来自日历的内容”+cal);
Date currentLocalTime=cal.getTime();
//System.out.println(“来自CurrentLocalTime的内容”+CurrentLocalTime);
日期格式日期=新的简化格式(“yyyy-MM-dd”);
date.setTimeZone(TimeZone.getTimeZone(“GMT”));
//System.out.println(“起始日期”+日期);
字符串localTime=date.format(currentLocalTime);
//localTime=“2014-08-26”;
System.out.println(“结果为==”+localTime);
pb.setVisibility(View.VISIBLE);
新建MyAsyncTask().execute(localTime,s2);
}
//@覆盖
//public void onBackPressed(){
//Log.d(“CDA”、“onBackPressed Called”);
//Intent setIntent=新的Intent(Intent.ACTION_MAIN);
//setIntent.addCategory(Intent.CATEGORY_HOME);
//setIntent.setFlags(Intent.FLAG\u活动\u新任务);
//星触觉(setIntent);
// }
私有类MyAsyncTask扩展了AsyncTask{
@凌驾
受保护的字符串doInBackground(字符串…参数){
//TODO自动生成的方法存根
日志d(“tag1”,“in do in”);
字符串s=postData(参数);
日志d(“tag2”,“SSS中的do”);
//这是第五次印刷
Log.d(“s是什么”,s);
返回s;
}
受保护的void onPostExecute(字符串结果){
日志d(“在岗”、“在岗执行”);
pb.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(),“显示约会”,Toast.LENGTH\u SHORT.show();
//Log.d(“标签”,结果);
初始(结果);
submit.setVisibility(View.GONE);
headerText.setVisibility(View.VISIBLE);
submit2.setVisibility(View.VISIBLE);
}
}
受保护的void onProgressUpdate(整数…进度){
pb.setProgress(progress[0]);
}
公共字符串postData(字符串值IwantToSend[]{
字符串OrigResponsetText=“”;
试一试{
//添加您的数据
List nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“CurrentDate”,valueIWantToSend[0]);
添加(新的BasicNameValuePair(“repname”,valueIWantToSend[1]);
System.out.println(“CurrentDate”+valueIWantToSend[0]);
System.out.println(“用户名”+valueIWantToSend[1]);
exampleString1=valueIWantToSend[0];
exampleString2=值IwantToSend[1];
HttpClient HttpClient=新的DefaultHttpClient();
HttpParams params=httpclient.getParams();
HttpConnectionParams.setConnectionTimeout(httpclient.getParams(),10000000);
//httppost=新的httppost(“http://192.168.56.1:8080/First/Hello");
httppost=新的httppost(“http://203.199.134.131:8080/First/Hello");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
/*执行*/
HttpResponse response=httpclient.execute(httppost);
System.out.println(“来自servlet的响应”+response.toString());
D/1:(15416): in the onclick
I/System.out(15416): what is from calendarjava.util.GregorianCalendar[time=1409392501700,areFieldsSet=true,lenient=true,zone=Asia/Calcutta,firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2014,MONTH=7,WEEK_OF_YEAR=35,WEEK_OF_MONTH=5,DAY_OF_MONTH=30,DAY_OF_YEAR=242,DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=5,AM_PM=1,HOUR=3,HOUR_OF_DAY=15,MINUTE=25,SECOND=1,MILLISECOND=700,ZONE_OFFSET=19800000,DST_OFFSET=0]
I/System.out(15416): and result is == 2014-08-26
D/tag1(15416): in do in
I/System.out(15416): CurrentDate2014-08-26
I/System.out(15416): usernameaditi
I/System.out(15416): [socket][1] connection /203.199.134.131:8080;LocalPort=36598(10000000)
I/System.out(15416): [CDS]connect[/203.199.134.131:8080] tm:10000 D/Posix(15416): [Posix_connect Debug]Process com.example.simplehttpgetservlet :8080
I/System.out(22364): [socket][/192.168.2.73:45340] connected
I/System.out(15416): [CDS]rx timeout:0
W/System.err(15416): rto value is too small
I/System.out(15416): >doSendRequest
I/System.out(15416): <doSendRequest
I/System.out(15416): response from servletorg.apache.http.message.BasicHttpResponse@423611d8