首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android文件读取

Android文件读取
EN

Stack Overflow用户
提问于 2012-04-24 23:18:49
回答 1查看 888关注 0票数 1
代码语言:javascript
复制
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的代码,它工作得很好,但我希望在try {} catch块之外打印Log.w("Hello",“123”+行);的值,当我这样做时,它会给我错误,我应该在我的LogCat中打印这个值!

EN

回答 1

Stack Overflow用户

发布于 2012-04-24 23:22:21

一旦离开try块,line就会超出作用域,因为它是在其中声明的。

要解决这个问题,只需将line的声明移到try块之外,当您离开该块时,它仍然可见。

代码语言:javascript
复制
String line;
try {
    input = new BufferedReader(new InputStreamReader(
    openFileInput("myfile")));
    StringBuffer buffer = new StringBuffer();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10300914

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档