我的Android应用程序仅适用于调试模式,但当我将应用程序转换为apk文件时无效。我发现只有json的问题。虽然我关闭了网络(即互联网)它运行良好但我转身在网络上并尝试打开我的应用程序会导致应用程序崩溃。
这是我的代码
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.StrictMode;
import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class MapList extends Activity {
GoogleMap Gmap;
GPSTracker gps;
JSONObject jobj;
EditText searchEdit;
double longitude;
double latitude;
Marker blueMarker,redMarker;
String Countryname;
Context con=MapList.this;
public static final String URl="http://www.dealsocl.com/mysql2json_SG.php";
RelativeLayout listRelative,mapRelative;
// public static final String URl="http://192.168.0.6/singapore_dealsocl/mysql2json_TM.php";
public static final String Tag_val="data";
public static final String Tag_dealid="dealid";
public static final String Tag_title="title";
public static final String Tag_price="price";
public static final String Tag_worth="value";
public static final String Tag_saving="saving";
public static final String Tag_saving_percent="saving_percent";
public static final String Tag_imageurl="imageurl";
public static final String Tag_link="link";
public static final String Tag_latitude="latitude";
public static final String Tag_longitude="longitude";
public static final String Tag_siteName="name";
public static final String Tag_price_sign="price_sign";
public static final String Tag_cat_seo="cat_seo_title";
ArrayList<String>lat_arry=new ArrayList<String>();
ArrayList<String>long_arry=new ArrayList<String>();
ArrayList<String>deal_tite=new ArrayList<String>();
ArrayList<Double> mydoublat=new ArrayList<Double>();
ArrayList<Double> mydoublong=new ArrayList<Double>();
ArrayList<HashMap<String, String>>details=new ArrayList<HashMap<String,String>>();
ArrayList<HashMap<String, String>> searchResults=new ArrayList<HashMap<String,String>>(details);
HashMap<String, String>map;
ListAdapter myAdapter;
ListView DataList;
ImageView nearby,search,home,account;
String s;
//String[] url={"http://www.dealsocl.com/mysql2json_SG.php","http://www.dealsocl.in/mysql2json_IN.php"};
ArrayList<String>url=new ArrayList( Arrays.asList( new String[]{"http://www.dealsocl.in/mysql2json_IN.php","http://www.dealsocl.com/mysql2json_SG.php","http://www.dealsocl.com/mysql2json_AE.php"} ) );
@SuppressLint("NewApi") @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maplist);
StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
gps = new GPSTracker(MapList.this);
ConnectivityManager connectivityManager = (ConnectivityManager)
con.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobileInfo =
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifiInfo.isConnected() || mobileInfo.isConnected()) {
// Toast.makeText(getApplicationContext(), "network connected", 1000).show();
if(gps.canGetLocation()){
latitude = gps.getLatitude();
longitude = gps.getLongitude();
Log.e("laaaaaaaaaatttttttiiiiiiii", ""+latitude);
Log.e("longiiiiiiiiiiiiiiiiii", ""+longitude);
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
}else{
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
// gps.showSettingsAlert();
Toast.makeText(getApplicationContext(), "Gps is not enabled", Toast.LENGTH_LONG).show();
Intent intent=new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
sendBroadcast(intent);
latitude = gps.getLatitude();
longitude = gps.getLongitude();
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
}
//<------------------------------------------------------------------------------------------------------------------------>
Geocoder geoCoder = new Geocoder(getBaseContext());
Log.e("latii1", ""+latitude);
Log.e("longiiiiiiiiiiiiiiiiii", ""+longitude);
List<Address> matches = null;
try {
Log.e("latiiii2", ""+latitude);
Log.e("longiiiiiiiiiiiiiiiiii", ""+longitude);
matches = geoCoder.getFromLocation( latitude, longitude, 1);
//Toast.makeText(getApplicationContext(),""+matches, 1000).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Address bestMatch = (matches.isEmpty() ? null : matches.get(0));
//String countryname=bestMatch.getCountryName().toString();
Toast.makeText(getApplicationContext(),"Your country name is:"+bestMatch.getCountryName(), 1000).show();
Countryname=bestMatch.getCountryName().toUpperCase().toString();
Log.e("address", ""+Countryname);
//<------------------------------------------------------------------------------------------------------------------------->
if(Countryname.equalsIgnoreCase("India")){
s=url.get(0).toString();
Log.e("country check1",s);
MyAsyncTask myAsyncTask=new MyAsyncTask();
myAsyncTask.execute(s);
}else if(Countryname.equalsIgnoreCase("SINGAPORE")){
s=url.get(1).toString();
Log.i("country check1", s);
MyAsyncTask myAsyncTask=new MyAsyncTask();
myAsyncTask.execute(s);
}else if(Countryname.equalsIgnoreCase("UNITED ARAB EMIRATES")){
s=url.get(2).toString();
Log.e("country check1", s);
MyAsyncTask myAsyncTask=new MyAsyncTask();
myAsyncTask.execute(s);
}else{
Toast.makeText(getApplicationContext(), "Currently no deals in your location", 1000).show();
}
}else{
RelativeLayout rl=(RelativeLayout)findViewById(R.id.realtivemap1);
RelativeLayout rl1=(RelativeLayout)findViewById(R.id.listrlayout2);
//TextView alertText=(TextView)findViewById(R.id.textAlert);
rl.setVisibility(View.VISIBLE);
rl1.setVisibility(View.INVISIBLE);
new Handler().postDelayed(new Runnable() {
public void run() {
finish();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
overridePendingTransition(R.animator.left_to_right, R.animator.slide_out_bottom);
}
}, 10000);
}
DataList=(ListView)findViewById(R.id.listView1);
searchEdit=(EditText)findViewById(R.id.searchEd);
mapRelative=(RelativeLayout)findViewById(R.id.realtivemap1);
listRelative=(RelativeLayout)findViewById(R.id.listrlayout2);
searchEdit.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
//clear the initial data set
String searchString=searchEdit.getText().toString();
int textLength=searchString.length();
//clear the initial data set
searchResults.clear();
for(int i=0;i<details.size();i++)
{
String titleString=details.get(i).get(Tag_title).toString();
if(textLength<=titleString.length()){
//compare the String in EditText with Names in the ArrayList
if(searchString.equalsIgnoreCase(titleString.substring(0,textLength)))
/* Toast.makeText(getApplicationContext(),playerName,1).show();*/
searchResults.add(details.get(i));
}
}
myAdapter=new LazyAdapter1(MapList.this, searchResults);
DataList.setAdapter(myAdapter);
((BaseAdapter) myAdapter).notifyDataSetChanged();
/* MapListActivity.this.myAdapter.getFilter().filter(s); */
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
DataList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
String title=((TextView) view.findViewById(R.id.getText_s_)).getText().toString();
String price=((TextView) view.findViewById(R.id.Price_s_)).getText().toString();
String savings=((TextView) view.findViewById(R.id.Saving_s_)).getText().toString();
String worth=((TextView) view.findViewById(R.id.Worth_s_)).getText().toString();
String discount=((TextView) view.findViewById(R.id.Discount_s_)).getText().toString();
String sitename=((TextView) view.findViewById(R.id.Site_n_)).getText().toString();
String imageText=((TextView) view.findViewById(R.id.ImageTextURl_s_)).getText().toString();
String lat=((TextView) view.findViewById(R.id.lat_s)).getText().toString();
String lng=((TextView) view.findViewById(R.id.lng_s)).getText().toString();
Log.i(Tag_title, title);
Log.i(Tag_price, price);
Log.i(Tag_saving, savings);
Log.i(Tag_worth, worth);
Log.i(Tag_saving_percent, discount);
Log.i(Tag_siteName, sitename);
Log.i(Tag_imageurl, imageText);
/*Log.i(Tag_latitude,""+lat);
Log.i(Tag_longitude,""+ lng);*/
Intent in = new Intent(getApplicationContext(), First.class);
in.putExtra(Tag_title, title);
in.putExtra(Tag_price, price);
in.putExtra(Tag_saving, savings);
in.putExtra(Tag_worth, worth);
in.putExtra(Tag_saving_percent, discount);
in.putExtra(Tag_siteName, sitename);
in.putExtra(Tag_imageurl, imageText);
in.putExtra(Tag_latitude, lat);
in.putExtra(Tag_longitude, lng);
startActivity(in);
}
});
}
class MyAsyncTask extends AsyncTask<String, Integer, ArrayList<HashMap<String, String>> > {
ProgressDialog dialog;
@Override
protected ArrayList<HashMap<String, String>> doInBackground(String... params) {
/* JSONObject json = JSONfunctions.getJSONfromURL(params[0]);*/
JsonParser parser=new JsonParser();
try {
jobj=parser.getJsonUrl(s);
Log.e("jobj", ""+jobj);
JSONArray array=jobj.getJSONArray(Tag_val);
/*Log.e("array", ""+array);*/
for(int i=0;i<array.length();i++){
/* Log.e("array", ""+array);*/
JSONObject c=array.getJSONObject(i);
/*Log.e("object", ""+c);*/
/*String id=c.getString(Tag_dealid);*/
String title=c.getString(Tag_title);
String price=c.getString(Tag_price);
String value=c.getString(Tag_worth);
String saving=c.getString(Tag_saving);
String sav_perc=c.getString(Tag_saving_percent);
/*String link=c.getString(Tag_link);*/
String lat=c.getString(Tag_latitude);
String longi=c.getString(Tag_longitude);
String image=c.getString(Tag_imageurl);
String siteName=c.getString(Tag_siteName);
String price_sign=c.getString(Tag_price_sign);
String cat_seo_title=c.getString(Tag_cat_seo);
Log.i("lat", lat);
Log.i("longi",longi);
lat_arry.add(lat);
long_arry.add(longi);
deal_tite.add(title);
Log.e("lat_arry",lat_arry.get(i));
Log.e("long_arry",long_arry.get(i));
map=new HashMap<String, String>();
//map.put(Tag_dealid, id);
map.put(Tag_title, title);
map.put(Tag_worth, value);
map.put(Tag_saving, saving);
map.put(Tag_saving_percent, sav_perc);
map.put(Tag_price,price);
map.put(Tag_siteName,siteName);
map.put(Tag_price_sign,price_sign);
// map.put(Tag_link, link);
map.put(Tag_latitude, lat);
map.put(Tag_longitude, longi);
map.put(Tag_imageurl, image);
map.put(Tag_cat_seo, cat_seo_title);
/*Double long1 = longitude;
Double lat1 = latitude;
String[] doublong= long_arry.toArray(new String[long_arry.size()]);
doublong=long_arry.toArray(doublong);
double[] doub_long = new double[doublong.length];
doub_long[i] = Double.parseDouble(doublong[i]);
mydoublong.add(doub_long[i]);
String[] doublat= lat_arry.toArray(new String[lat_arry.size()]);
doublong=lat_arry.toArray(doublong);
double[] doub_lat = new double[doublat.length];
doub_lat[i] = Double.parseDouble(doublat[i]);
mydoublat.add(doub_lat[i]);
Double d=DistanceBetweenPlaces(longitude,latitude, mydoublong.get(i), mydoublat.get(i));
String distance=new Double(d).toString();
Log.w("Distance", ""+distance);
map.put("dist", distance);*/
details.add(map);
}
return details;
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
return null;
}
@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
myAdapter=new LazyAdapter1(MapList.this, details);
DataList.setAdapter(myAdapter);
dialog.dismiss();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(MapList.this);
dialog.setMessage("Your deals are loading....");
/* dialog.setIndeterminate(false);*/
dialog.setCancelable(false);
dialog.show();
}
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
finish();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
overridePendingTransition(R.animator.left_to_right, R.animator.slide_out_bottom);
}
}
这是我的json解析器代码:
public class JsonParser {
JSONObject jobj;
String json="";
InputStream is=null;
public JSONObject getJsonUrl(String url){
DefaultHttpClient client=new DefaultHttpClient();
HttpPost post=new HttpPost(url);
try {
HttpResponse response=client.execute(post);
HttpEntity entity=response.getEntity();
is=entity.getContent();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
BufferedReader br=new BufferedReader(new InputStreamReader(is, "iso-8859-1"),8);
StringBuilder sb=new StringBuilder();
String line=null;
try {
while((line=br.readLine())!=null){
sb.append(line+"\n");
}
is.close();
json=sb.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
jobj=new JSONObject(json);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jobj;
}
}
并且重要的是如果我转换成apk文件并将apk文件运行到设备中,这是我用于调试设备工作正常的应用程序(即apk可以在deveice上运行)但其他设备它不会工作(即它不会打开或不在设备上运行)
任何人都可以解决这个问题吗?
答案 0 :(得分:0)
在Proguard File中添加此行
-keep class org.json.** { *;}
Proguard混淆了JSON Object类。在调试版本中,通常会禁用Proguard,因此不会发生此问题。