1
package com.callout.project;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import com.callout.project.Mylocation.LocationResult;



import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class WorksActivity extends Activity  {
/** Called when the activity is first created. */
Mylocation myLocation = new Mylocation();
TextView rtv;
String id;
String strloc;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register);

    Bundle ext = getIntent().getExtras();
       id = (String) ext.get("registration_id");
      Log.w("rid", id);
    findCurrentLocation();
    rtv = (TextView)findViewById(R.id.rtv);
    rtv.setText("Thank you For Registering");
    String eol = System.getProperty("line.separator");
    BufferedReader input = null;
    try {
      input = new BufferedReader(new InputStreamReader(
        openFileInput("myfile")));
       String line;
      StringBuffer buffer = new StringBuffer();
      while ((line = input.readLine()) != null) {
        buffer.append(line + eol);
        Log.w("Hello","123"+line);
      }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
    if (input != null) {
      try {
    input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
      }
    }

//Log.w("file","msg"+line);


//  sendtoserver(id,strloc);
}

private void findCurrentLocation() {
    myLocation.getLocation(this, locationResult);
}
public LocationResult locationResult = new LocationResult() {

    @Override
    public void gotLocation(Location location) {
        // TODO Auto-generated method stub
        if (location != null) {
             strloc  = location.getLatitude() + ","
                    + location.getLongitude();
             Log.w("works",strloc);


    // TODO Auto-generated method stub
    AlertDialog malert = new AlertDialog.Builder(WorksActivity.this).create();
       malert.setTitle("here"+strloc);     
       Log.w("func","in func"+id);
     HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.funnystrippers.co.cc/test.php");

        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
            nameValuePairs.add(new BasicNameValuePair("Rid", "56"));
            nameValuePairs.add(new BasicNameValuePair("location","strloc"));
            nameValuePairs.add(new BasicNameValuePair("content", "hello frm android"));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = httpclient.execute(httppost);
            Log.w("res",response.toString());

        } catch (ClientProtocolException e) {
        } catch (IOException e) {
        }

        malert.show();
}
        }
    };

}

C2DM を扱う次のコードがありますが、正常に動作していますが、Log.w("Hello","123"+line); の値が必要です。try {} catch ブロックの外側に出力すると、エラーが発生します。LogCat に出力された値を取得する必要があります !!!

4

1 に答える 1

1

line内部で宣言されているため、try ブロックを離れると範囲外になります。

それを修正するには、try ブロックの外側に宣言を移動するだけでline、ブロックを離れても表示されます。

String line;
try {
    input = new BufferedReader(new InputStreamReader(
    openFileInput("myfile")));
    StringBuffer buffer = new StringBuffer();
于 2012-04-24T15:22:20.860 に答える