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 块,它就会超出范围,因为它是在其中声明的。

要修复它,只需将声明移到linetry 块之外,离开该块后它仍然可见。

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