1.package com.example.user.batroomapp;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.example.user.batroomapp.gsontopojo.MyPojo;
import com.example.user.batroomapp.gsontopojo.Routes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
我是从Json到Gson的班级,现在我不知道如何继续。请帮助我卡住。
公共类DirectionJson扩展了AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_fragment);
}
String apiKeyDirection = "My Api Direction";
public static class JsonTask extends AsyncTask <String, String, List<Routes>> {
@Override
protected List<Routes> doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
URL url;
{
try {
url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer=new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
Log.d("aaa",""+line);
}
Gson gson=new Gson();
MyPojo myPojo =gson.fromJson(line,MyPojo.class);
for (Routes routes:myPojo.getRoutes());
String finalJason = buffer.toString();
JSONObject parentObject = new JSONObject(finalJason);
JSONArray parentArrey = parentObject.getJSONArray("routes");
List<Routes>routeslist = new ArrayList<>();
// JSONArray parentArrey2 = parentObject.getJSONArray(&#34; geocoded_waypoints&#34;); for(int i = 0; i
routeslist.add(routes);
}
return routeslist;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
@Override
protected void onPostExecute(List<Routes> result) {
super.onPostExecute(result);
Log.d("json",result.toString());
Gson gson = new GsonBuilder().create();
// MyPojo myPoj = gson.fromJson(result, MyPojo.class);
//Log.d("bbb", myPoj.toString());
}
private void parseJson(String data){
if (data==null)
return;
}
}
}
这是点击
protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState);
setContentView(R.layout.map_fragment);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
final SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.mapFragment);
mapFragment.getMapAsync(this);
Button bDriving = (Button) findViewById(R.id.bDriving);
bDriving.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DirectionJson.JsonTask().execute
("https://maps.googleapis.com/maps/api/directions/json?origin=sydney" +
",au&destination=perth,au&waypoints=via:-37.81223%2C144.96254%7Cvia:" +
"-34.92788%2C138.60008&key=" + apiKeyDirection);
}
});