在我使用的大部分代码中都有可怕的东西,比如:
String url = "../Somewhere/SomeServlet?method=AMethod&id="+object.getSomething()+ "&aParam="+object.getSomethingElse());
Run Code Online (Sandbox Code Playgroud)
或者 - 更糟糕的是:
String url = "Somewhere/Here/Something.jsp?path="+aFile.toString().replace("\\","/")+ "&aParam="+object.getSomethingElse());
Run Code Online (Sandbox Code Playgroud)
有没有正确的方法:
从本质上讲 - 构建字符串要比正确地完成字符串要容易得多.有没有办法正确地做到这一点就像建立字符串一样简单?
添加
为了清楚起见 - 经过一番思考 - 我想我正在寻找类似的东西:
String s = new MyThing()
.setPlace("Somewhere/Something.jsp")
.addParameter(aName,aValue)
.addParameter(aName,aFile)
.toString();
Run Code Online (Sandbox Code Playgroud)
因此它将处理转义的所有不愉快并添加"?"/"&"并将"\"更改为"/"而不是使用"\"作为文件等.
如果我必须自己编写一个(即如果Apache不是一个选项),那么有真正的Java技术可以正确地转义各个部分.我的意思是在参数中将""转换为"".而逃避""在其他地方"%20".
我在Windows Mobile上使用紧凑框架/ C#.
在我的应用程序中,我通过序列化对象并使用HttpWebRequest/POST请求将数据上传到服务器来上传数据.在服务器上,后置数据被反序列化并保存到数据库.
前几天我意识到我在帖子数据(&符号等)中遇到了特殊字符的问题.所以我在方法中引入了Uri.EscapeDataString(),一切都很顺利.
但是,今天我发现当应用程序尝试上传大量数据时出现问题(我不确定此刻究竟表示"大"的是什么!)
现有代码(种类)
var uploadData = new List<Things>();
uploadData.Add(new Thing() { Name = "Test 01" });
uploadData.Add(new Thing() { Name = "Test 02" });
uploadData.Add(new Thing() { Name = "Test with an & Ampersand " }); // Do this a lot!!
var postData = "uploadData=" + Uri.EscapeDataString(JsonConvert.SerializeObject(uploadData, new IsoDateTimeConverter()));
Run Code Online (Sandbox Code Playgroud)
问题
对Uri.EscapeDataString()的调用导致以下异常:
System.UriFormatException:无效的URI:Uri字符串太长.
题
有没有其他方法来准备上传数据?
据我所知,HttpUtility(它有自己的Encode/Decode方法)不适用于紧凑的框架.
我正在做一个Windows Service.在Service每晚都要donwload东西,为此我要放置URI在App.config万一我以后需要改变它.
我想在App.Config中编写一个URI.什么使它无效,我应该如何处理?
<appSettings>
<add key="fooUriString"
value="https://foo.bar.baz/download/DownloadStream?id=5486cfb8c50c9f9a2c1bc43daf7ddeed&login=null&password=null"/>
</appSettings>
Run Code Online (Sandbox Code Playgroud)
我的错误:
- Entity 'login' not defined
- Expecting ';'
- Entity 'password' not defined
- Application Configuration file "App.config" is invalid. An error occurred
Run Code Online (Sandbox Code Playgroud) 所以我试图在URL中使用这个字符串: -
http://site-test.collercapital.com/Meetings/IC/DownloadDocument?meetingId=c21c905c-8359-4bd6-b864-844709e05754&itemId=a4b724d1-282e-4b36-9d16-d619a807ba67&file=\\s604132shvw140\Test-Documents\c21c905c-8359-4bd6-b864-844709e05754_attachments\7e89c3cb-ce53-4a04-a9ee-1a584e157987\myDoc.pdf
Run Code Online (Sandbox Code Playgroud)
在这段代码中: -
String fileToDownloadLocation = //The above string
URL fileToDownload = new URL(fileToDownloadLocation);
HttpGet httpget = new HttpGet(fileToDownload.toURI());
Run Code Online (Sandbox Code Playgroud)
但在这一点上我得到错误: -
java.net.URISyntaxException: Illegal character in query at index 169:Blahblahblah
Run Code Online (Sandbox Code Playgroud)
我通过一些谷歌搜索意识到这是由于URL中的字符(猜测&),所以我在一些代码中添加,所以它现在看起来像这样: -
String fileToDownloadLocation = //The above string
fileToDownloadLocation = URLEncoder.encode(fileToDownloadLocation, "UTF-8");
URL fileToDownload = new URL(fileToDownloadLocation);
HttpGet httpget = new HttpGet(fileToDownload.toURI());
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试运行它时,当我尝试创建URL时出现错误,然后错误如下: -
java.net.MalformedURLException: no protocol: http%3A%2F%2Fsite-test.collercapital.com%2FMeetings%2FIC%2FDownloadDocument%3FmeetingId%3Dc21c905c-8359-4bd6-b864-844709e05754%26itemId%3Da4b724d1-282e-4b36-9d16-d619a807ba67%26file%3D%5C%5Cs604132shvw140%5CTest-Documents%5Cc21c905c-8359-4bd6-b864-844709e05754_attachments%5C7e89c3cb-ce53-4a04-a9ee-1a584e157987%myDoc.pdf
Run Code Online (Sandbox Code Playgroud)
在我创建了URL之后看起来我不能进行编码,否则它会替换斜杠和不应该的东西,但是我看不出如何用字符串创建URL然后将其格式化它适合使用.我对这一切并不是特别熟悉,并希望有人能够指出我错过了将字符串A放入适当格式化的URL然后使用正确的字符替换?
任何建议都非常感谢!
与在Android中发送暂停和DTMF输入类似,我正在尝试将暂停字符","发送到拨号器.这适用于HTC Sense手机甚至Xoom,但不适用于Nexus One或T-Mobile G2等"股票体验"手机(我怀疑是摩托罗拉Droid).
这些手机似乎有一个拨号器,试图对数字进行漂亮格式化(即添加破折号),并在点击逗号字符时停止.有趣的是,它不会阻塞"p"字符,尽管它会删除"p"并继续添加数字.
这是ActivityManager看到的内容:
I/ActivityManager( 92): Starting activity: Intent { act=android.intent.action.DIAL dat=tel:8883333,444 cmp=com.android.contacts/.DialtactsActivity }
Run Code Online (Sandbox Code Playgroud)
我也尝试了编码形式,"tel:8883333%2C444",这些手机的行为没有区别.我已经尝试了"p",如上所述,但是这些字符被删除导致拨号器888-333-3444填充不正确,并且我不确定"p"是否正确.
所以,问题是:有没有办法指定一个适用于大多数或所有Android拨号程序的暂停?
我真的很沮丧地解决了我的问题,我有一个使用相机的应用程序,当相机拍摄照片时,它会显示在活动上,当我不使用cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mPhotoUri);照片时会显示活动,但名称和路径文件不是喜欢我想要的.我正在使用galaxy选项卡进行编译,我已经按照教程从这里解决了我的问题,这里和这里但我的应用程序仍然强制关闭和错误,这是我的代码:
{
File sdCard= Environment.getExternalStorageDirectory();
File path = new File (sdCard.getAbsolutePath() + "/android/data/spaj_foto");
path.mkdir();
File file= new File (path,"spaj_foto.png");
String filePath ="/android/data/spaj_foto/spaj_foto.png";
try {
file.createNewFile();
} catch (IOException e) {}
mPhotoUri = Uri.fromFile(file);
values.put( MediaStore.Images.ImageColumns.DATA, file.getPath());
mPhotoUri=getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mPhotoUri);
startActivityForResult(cameraIntent,TAKE_PHOTO_CODE );
}
Run Code Online (Sandbox Code Playgroud)
activityresult方法:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
Bitmap photo = …Run Code Online (Sandbox Code Playgroud) 在我正在使用的环境(Tomcat 6)中,路径段中的百分比序列显然在映射到@PathVariable时使用ISO-8859-1进行解码.
我希望它是UTF-8.
我已经将Tomcat配置为使用UTF-8(使用server.xml中的URIEncoding属性).
Spring/Rest是自己进行解码吗?如果是,我可以在哪里覆盖默认编码?
附加信息; 这是我的测试代码:
@RequestMapping( value = "/enc/{foo}", method = RequestMethod.GET )
public HttpEntity<String> enc( @PathVariable( "foo" ) String foo, HttpServletRequest req )
{
String resp;
resp = " path variable foo: " + foo + "\n" +
" req.getPathInfo(): " + req.getPathInfo() + "\n" +
"req.getPathTranslated(): " + req.getPathTranslated() + "\n" +
" req.getRequestURI(): " + req.getRequestURI() + "\n" +
" req.getContextPath(): " + req.getContextPath() + "\n";
HttpHeaders headers = new HttpHeaders();
headers.setContentType( new MediaType( "text", …Run Code Online (Sandbox Code Playgroud) 可能重复:
Android:获取资源中文件的大小?
我想将文件上传到WebServer.要显示上传的进度,我需要知道文件的完整大小.因为我只有文件的Uri(从Intent获得),所以我没有机会获得文件的大小.我的解决方法是打开一个InputStream并计算在此InputStream上读取的字节数.但这很迂回.是否有更简单/传统的方法来解决这个问题?
谢谢
在URI的查询部分中指定布尔值的首选方法是什么?普通的查询字符串看起来像
a=foo&b=bar
Run Code Online (Sandbox Code Playgroud)
假设我有一个带有布尔值的参数"c",我应该说明
a=foo&b=bar&c=1
Run Code Online (Sandbox Code Playgroud)
要么
a=foo&b=bar&c=True
Run Code Online (Sandbox Code Playgroud)
要么
a=foo&b=bar&c=true
Run Code Online (Sandbox Code Playgroud)
我检查了RFC 2396的查询组件部分,但没有指定如何表示布尔参数.所以我想知道的是通常(或合理)的方式是什么?
从Lollipop开始,应用程序可以访问真正的SD卡(在Kitkat上无法访问之后,并且在之前的版本中尚未获得官方支持),正如我在此处所述.
因为看到支持SD卡的Lollipop设备变得非常罕见,并且因为模拟器实际上没有能力(或者它?)来模拟SD卡支持,所以我花了很长时间来测试它.
无论如何,似乎不是使用普通的File类来访问SD卡(一旦获得了它的许可),你需要使用Uris来使用它,使用DocumentFile.
这限制了对正常路径的访问,因为我找不到将Uris转换为路径的方法,反之亦然(加上它非常烦人).这也意味着我不知道如何检查当前的SD卡是否可访问,因此我不知道何时要求用户允许读取/写入(或向他们).
目前,这就是我获取所有SD卡的路径:
/**
* returns a list of all available sd cards paths, or null if not found.
*
* @param includePrimaryExternalStorage set to true if you wish to also include the path of the primary external storage
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static List<String> getExternalStoragePaths(final Context context,final boolean includePrimaryExternalStorage)
{
final File primaryExternalStorageDirectory=Environment.getExternalStorageDirectory();
final List<String> result=new ArrayList<>();
final File[] externalCacheDirs=ContextCompat.getExternalCacheDirs(context);
if(externalCacheDirs==null||externalCacheDirs.length==0)
return result;
if(externalCacheDirs.length==1)
{
if(externalCacheDirs[0]==null)
return result;
final String storageState=EnvironmentCompat.getStorageState(externalCacheDirs[0]);
if(!Environment.MEDIA_MOUNTED.equals(storageState))
return …Run Code Online (Sandbox Code Playgroud)