在单独的字符串

时间:2016-02-05 18:27:49

标签: php android

我已经在数据库中存储了经度和纬度,并以单个字符串的形式通过http请求获取它们。现在我想开始一个地图活动,它从服务器响应中获取经度和纬度,并在该点放置一个标记。问题是我无法获得如何将经度和纬度分开,因为它们是从服务器返回的单个字符串中。这是我的活动:

public class Signin extends AsyncTask<String,Void,String> 
{
  private Context context;
  private TextView roleField;
  private Context context1;

  public Signin(Context context) {
    this.context = context;
  }
  protected void onPreExecute() {}
  @Override
  protected String doInBackground(String... arg0) {
    try {
      String bus_id = (String) arg0[0];
      String link = "http://mysystem_ip/routes.php?bus_id=" + bus_id + "";
      URL url = new URL(link);
      HttpClient client = new DefaultHttpClient();
      HttpGet request = new HttpGet();
      request.setURI(new URI(link));
      HttpResponse response = client.execute(request);
      BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
      StringBuffer sb = new StringBuffer("");
      String line = "";

      while ((line = in.readLine()) != null) {
        sb.append(line);
        break;
      }
      in.close();
      return sb.toString();
    }
    catch (Exception e) {
      return new String("Exception: " + e.getMessage());
    }
  }

  @Override
  protected void onPostExecute(String result) {
    this.roleField.setText(result);
    Intent i1 = new Intent (context, MapsActivity.class);
    i1.putExtra("result",result);
    context.startActivity(i1);
  }
}

1 个答案:

答案 0 :(得分:1)

分割此String的快速而肮脏的方法是这样的:

String response = "31.578369901154527 74.35708886792317";
String[] responseParts = response.split(" ");
String firstPart = responseParts[0];
String secondPart = responseParts[1];
Log.d("SPLIT RESPONSE", "first: "+firstPart+"  second: "+secondPart);

这段代码当然假设分隔字符串两部分的分隔符是一个空格" " - 只要不会改变,你应该能够将响应字符串拆分为漂亮的轻松使用上面的代码。